Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jQuery beginner ".each()"

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

$("#menu a").each(function(){ 
  console.log(($this.attr("href"));
});
<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>

2 Answers

James Spence
James Spence
17,563 Points

You made a typo with 'this', your code should read:

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

James has it. "Unexpected token" errors are SUPER common, always carefully check over your brackets and parentheses carefully. It nearly always means you have some tiny syntactic error.