my html looks like this
- Item 1
- Item 2
- Item
You need to keep track of the list of <li>
elements and then use that to determine the index:
var $all_lis = $('li');
$all_lis.on('click', function() {
var index = $all_lis.index(this);
alert(index);
});
Demo
The first item will give 0
, the last item will give 8
(i.e. 9th item).
this
inside the callback function is just what you want, the clicked item.
You want to get index of selected ul
element? Try this:
var parentInd = $(this).parent().parent().index();