get an element position using jquery

javascript, jquery

Solution

Use index() to get the position. It gives you `zero-based index` so you will get `0` for first element.

Live Demo

$('li').click(function(){    
    alert($(this).index())
});

Problem

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

Original source