31 Oct 2011

Get the index of an element in an unordered set

JQUERY snippet to find the index of an item.

In this case if a list item is clicked, we get the index of the element in relation to it's parent container.

$("ul > li").click(function () { 
    var index = $(this).prevAll().length; 
});