JavaScript jQuery Basics Working with jQuery Collections The .each() method

Seth Pabst
Seth Pabst
7,673 Points

Can anyone point out the syntax error in this problem?

Don't know what I'm doing wrong

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

1 Answer

Steven Parker
Steven Parker
173,682 Points

The "this" inside an "each" refers to a DOM element. So to be able to call jQuery methods, you need to convert it to a jQuery object ("$(this)").

Seth Pabst
Seth Pabst
7,673 Points

Oh I see thanks:)