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
  • 1,031
  • 4
  • 14
  • 33

5 Answers5

4

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

$("#menu a").each(function(){
    console.log($(this).attr("href"));
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
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
  • 33,140
  • 10
  • 61
  • 78
0

Try this Working Demo

$("#menu ul li a").each(function(){ 
console.log(($(this).attr("href")));
})
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
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,166
  • 4
  • 25
  • 31
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,303
  • 2
  • 27
  • 42