I’d like to find an element position using JQuery. Not the X and Y position using .position .
For exemple
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
And the js
//return the li pos
$('li').click(function(){...})
for exemple if I click the third li the function return the li pos witch is 2
I try something but it doesn’t works
$('li').click(function(){
for(var i = 0; i < $('li').length; i++){
if($('li:eq('+i+')') == $(this)){
alert(i);
return true;
}
}
alert('fail');
return false;
})
And I always get fail
Thanks
Use index() to get the position. It gives you
zero-based indexso you will get0for first element.Live Demo