Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
220,598 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
220,598 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
220,598 Points

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

Steven Parker
Steven Parker
220,598 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