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 Basics Working with jQuery Collections The .each() method

omer syed
omer syed
8,634 Points

Bummer: Did you delete or comment out the provided JavaScript code?

Hey so i feel like my code is correct, but i keep getting this error not sure why. Maybe its a bug?? Anyway I'm gonna continue on, if anyone has seen this before let me know if it has something to do with my code. Thanks!!!

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h2>My Favorite Things</h2>

    <ul class="favorite-things">
        <li>Kittens</li>
        <li>Rainbows</li>
        <li>Unicorns</li>
        <li>Sprinkles</li>
    </ul>

    <script
    src="jquery-3.2.1.min.js"></script>
    <script src="app.js"></script>
</body>
</html>
app.js
$('.favorite-things li').each(function(this) {
  $(this).prepend('<input type="checkbox"/>');
});

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

The problem is where you're giving this as an parameter to the function. The function will automatically have a this, and you can't call a parameter this because of this word's existing special meaning. The function you pass to .each can take a parameter for the index, but in this case we don't need it.

$('.favorite-things li').each(function() {
  $(this).prepend('<input type="checkbox"/>');
});

I think Treehouse's error message was a little misleading in this case. The JavaScript interpreter was thrown off by this error, but somehow Treehouse came to the conclusion that there wasn't any JavaScript at all. Sometimes there are some quirks stemming from how they handle evaluating the code behind the scenes.

omer syed
omer syed
8,634 Points

Thank you so much!!!