I have a standard list.
<ul>
<li><a href="#">blah 1</a></li>
<li><a href="#">blah 2</a></li>
<li><a href="#">blah 3</a></li>
<li><a href="#">blah 4</a></li>
</ul>
And my jQuery:
$('ul li a').live('click', function() {
var parent = $(this).parent('li');
});
What I want to find out is the parent li's position in the list of the clicked link e.g. clicking on blah 3 would give me 2, blah 4 would give 3 etc.
$('ul li a').live('click', function() {
console.log($(this).parent('li').index());
});
Will give you what you want, but keep in mind these are 0 based indexes -- ie the first line item is index 0, the last line item is 3.
jQuery index() method documentation
you can get the index of an element with jquery`s index
$('ul li a').live('click', function()
{
var index = $(this).index();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With