I need to find a specific elements position in a jQuery collection.
For example in a collection of a elements I need to know the numeric position of the element with the “active” class.
<ul>
<li><a href="#"></a></li>
<li><a class="active" href="#"></a></li>
<li><a href="#"></a></li>
</ul>
I assumed using index would be the way to go but the following returns 0 no matter what the location of active.
$('ul li a').index('.active');
Is there any simple way to do what this?
Try this:
Note that even by using correct syntax this code always returns
0as there is only one anchor link within li tags, you can find the index of parent li element instead.