I have something like this:
<div>
<ul id="ul_element_id">
<li>
<a id="first"></a>
</li>
<li>
<a id="second"></a>
</li>
<li>
<a id="third"></a>
</li>
</ul>
</div>
Is there a quick way to figure out $(‘#first’) is the first guy in relation to other elements?
I think you are looking for something like the following:
That will return 0 since “#first” is the first in the list. If you did:
you would get 1. If you wanted you could just add 1 to whatever value is returned as long as the returned value is not -1.
Also you can change the $(“a”) selector to whatever you wanted. Something like $(“#ul_id a”) would work just fine too.
You can pass any selector to the index method to search a collection and have it return the index of the found element.
http://api.jquery.com/index/