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

Jack Cummins
Jack Cummins
17,417 Points

Please help me. P.S. I give out best answers if you give me a good answer!

The asignment is: Inside the jQuery each() method, use prepend() to add an '<input type="checkbox"/>' to the beginning of each list item. Use the this keyword, so the callback function doesn't need any arguments.

Thank you so much! Jack Cummins

P.S. Don't forget about the best answer! P.P.S. I also give out upvotes!

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() {
  const $checkbox = $('<input type="checkbox"/>');
  $(this).prepend($checkbox);
});

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Sometimes you can do things inside a challenge that might work outside, but will not work in the challenge because they don't meet the exact criteria.

Take a look again at the Bummer! message:

Within the anonymous function passed to the "each" method call, did you supply the argument "'<input type="checkbox"/>'" to the "prepend" method call on the jQuery object with an argument of "this"? Restart Preview Get Help Recheck work

Now, you are sending in the value of the variable checkBox to the prepend function, but it wants you to send in that string, not a variable holding that string. It's always a great idea to try not to do anything not explicitly asked for by the challenge.

This is the only line you needed:

 $(this).prepend('<input type="checkbox"/>');

:bulb: Hint: If you think you might be over-complicating something on one of these challenges, you are likely correct.

Hope this helps! :sparkles:

$(this).prepend('<input type="checkbox"/>');

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

});

why,it is not working ?