Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current <li> number

I have a list like this one:

<ul>

  <li>
    <a href="..."> ... </a>
    <a href="..."> ... </a>
  </li>

  <li>
    <a href="..."> ... </a>
    <a href="..."> ... </a>
  </li>

  <li>
    <a href="..."> ... </a>
    <a href="..."> ... </a>
  </li>

  ...

</ul>

and the jQuery:

$("li").each(function(){
     // do stuff
});

How can I get the current list number (like 1, 2 or 3) inside that jquery function (where do stuff is)?

like image 998
Alex Avatar asked Jan 21 '23 14:01

Alex


1 Answers

The callback function passed to each has two arguments, the first being the index you're looking for:

$('li').each(function(index, value) {
  // index is what you're looking for
});

Have a look at the documentation for each:

the callback is passed an array index and a corresponding array value each time.

Note that the index will be zero-based, so if you want your "1, 2, 3..." (from your question), you'll need to make the appropriate accommodations.

like image 183
Rob Hruska Avatar answered Jan 30 '23 21:01

Rob Hruska