The previous answer is working fine. 2 additions:
When your list contains a list of <a> elements, for example:
<ul id="test"> <li><a href="#">Element 1</a></li> <hr /><hr /> <li><a href="#">Element 2</a></li> <li><a href="#">Element 3</a></li> </ul>
then you should use var index = $(this).parent().parent().children().index(this);
As in the previous example, the list contains other elements, such as <hr /> , than you can use the filter in var index = $(this).parent().parent().children("li").index(this); to give you index 2 for "element 3" instead of index 4.
Jan 10 May '15 at 21:20 2015-05-10 21:20
source share