How can I detect the position of a specific item in the list ?
I have the following list:
<ul>
<li>
<li>
<li class="current">
<li>
</ul>
I need to know in which position the current item is. In this case the result should be 3.
thanks
How can I detect the position of a specific item in the list ?
I have the following list:
<ul>
<li>
<li>
<li class="current">
<li>
</ul>
I need to know in which position the current item is. In this case the result should be 3.
thanks
You can use:
$("ul li.current").index() + 1;
Noe that this is not the accepted answer in the other question, the situation is different when dealing with siblings. .index() (without arguments) gives the sibling-index, which is all you need here.
Edit: just re-read the question, this is 0-based so it'd result in 2 not 3, just add 1 to the .index() result if you're after 1-based numbering.
If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.