0

Why do I get an error in the console? "Uncaught SyntaxError: Unexpected token ;"

jQuery:

$("#menu a").each(function(){ 
console.log(($this.attr("href"));
});

HTML:

<body>
<div id="menu">
    <ul>
        <li class="selected"><a href="index.html">Home</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="contact.html">Contact</a></li>
        <li><a href="support.html">Support</a></li>
        <li><a href="faqs.html">FAQs</a></li>
        <li><a href="events.html">Events</a></li>
    </ul>
</div>

Alexey Tseitlin
  • 871
  • 1
  • 11
  • 28

5 Answers5

4

You have mis-matched brackets, and $this should be $(this):

$("#menu a").each(function(){
    console.log($(this).attr("href"));
});
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
1

It should be $(this) and not $this.

And also you have an extra parentheses opened inside console.log

Something like this

$("#menu a").each(function(){ 
  console.log($(this).attr("href"));
});
Dhiraj
  • 32,144
  • 8
  • 59
  • 77
0

Try this Working Demo

$("#menu ul li a").each(function(){ 
console.log(($(this).attr("href")));
})
I'm Geeker
  • 4,646
  • 5
  • 20
  • 40
0

Simply use this.href

$("#menu a").each(function(){
    //to grab the absolutel URL
    console.log( this.href );
    //To get the string from href
    console.log ( $(this).attr("href") );
});
lshettyl
  • 8,016
  • 4
  • 23
  • 30
0

$this will return that kind of error since it's not defined while $(this) refers to each individual link (<a href="#">) and will work just fine.

prototype
  • 3,163
  • 2
  • 25
  • 42