JavaScript jQuery Basics Understanding jQuery Events and DOM Traversal DOM Traversal with jQuery

Fabian Pijpers
PRO
Fabian Pijpers
Pro Student 31,559 Points

I have a problem with jquery challenge 2 of 2 '$('li').eq(2).prev(-2);'

Why does it not work?

$('li').eq(2).prev(-2); As you can see i have correctly called the .prev selector after the .eq() selector. Does anybody see where i went wrong.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
  <h2>Student List</h2>

  <ul class="student-list">
    <li>James McAvoy</li>
    <li>Alena Holligan</li>
    <li>Wade Christensen</li>
    <li>Matt Krzyzynski</li>
  </ul>

  <script src="jquery-3.2.1.min.js"></script>
  <script src="app.js"></script>
</body>
</html>
app.js
$('li').eq(2).prev(-2);

2 Answers

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there, Fabian Pijpers ! Looks like you're doing terrific and you clearly have the hang of the eq() method. But in contrast to that method, the prev() does not take an index or positional argument like you've passed. You can however, pass in a selector. The easiest way to move two up here is to run the .prev() twice with no arguments.

I think you can get it with this hint, but let me know if you're still stuck! :sparkles:

Fabian Pijpers
PRO
Fabian Pijpers
Pro Student 31,559 Points

Thanks Jennifer for noting that, at least now i can look further without repeating that perticular mistake. At least you tought me not to use indexing at the .prev() method again. Again Thank you.