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

kingdavid igbayilola
kingdavid igbayilola
8,678 Points

jquery .each() method

getting an error

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() {
  // Write code here
  const list = $(this).attr('li');
  $(this).parent().prepend('<input type="checkbox"/>');
});

2 Answers

Steven Parker
Steven Parker
231,261 Points

You're pretty close, but I see two issues:

  • the assignment of "list" is unnecessary (and it is not used)
  • the "parent" is not needed, "prepend" should be applied directly to $(this)

For future questions, please describe the issue in more detail.

kingdavid igbayilola
kingdavid igbayilola
8,678 Points

why is the parent not needed and the list variable

Steven Parker
Steven Parker
231,261 Points

You created "list", but you weren't using it for anything.

And the task asks you to add a checkbox to the beginning of each list item, and the list item is what "this" represents.

Also, if you take the "parent" you get the list itself (ul), and it's not correct HTML to add anything to a list other than another list item.

Steven Parker
Steven Parker
231,261 Points

I don't know what you mean, what's a "tree house group"?

Steven Parker
Steven Parker
231,261 Points

That must be related to the kind of subscription you have.
When I go to that link, I see "Bummer! You must be an organization administrator.".

Abdul Azeez
Abdul Azeez
3,884 Points

hey man, whats up...are you nigerian