Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find element nth position within parent

Tags:

jquery

If I have this:

<ul>
    <li><a href="#"></a>a</li>
    <li><a href="#"></a>b</li>
    <li><a href="#"></a>c</li>
    <li><a href="#"></a>d</li>
    <li><a href="#"></a>e</li>
</ul>

And lets say I clicked on third li item (with a content of 'c'). How Could I get the info this item is third within 'ul' parent?

like image 949
Toniq Avatar asked Apr 28 '12 20:04

Toniq


1 Answers

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

Demo here: http://jsfiddle.net/KyTP5/

Note that this gives you the zero-based index.

like image 154
Marc Avatar answered Nov 15 '22 23:11

Marc