-1

Possible Duplicate:
Get parent index with jQuery

If I click on li a - I want to get a index number of li

For example:

<ul class="tabs">
    <li><a class="active" href='#'>One</a></li>
    <li><a href='#'>Two</a></li>
    <li><a href='#'>Three</a></li>
</ul>

I have tried the jQuery code, it dont seem to work?

$("ul.tabs li a").on("click",function() {
        var li = $(this).prev();
        alert($(li).index(this));
});
Community
  • 1
  • 1
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
  • This isn't the problem, but you don't need to wrap jQuery objects with `$()`. Eg: `$(li).index(this)` could be `li.index(this)`. – Christian Nov 30 '12 at 15:45

2 Answers2

4

You need .parent() not .prev()

var li = $(this).parent();
alert( li.index() );

DEMO

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
0

this one worked for me; http://jsfiddle.net/mqdAe/

HTML

<ul class="tabs">
    <li><a class="active" href='#'>One</a></li>
    <li><a href='#'>Two</a></li>
    <li><a href='#'>Three</a></li>
</ul>

JS

$("ul.tabs li a").on("click",function() {
        var li = $(this).parent();
        alert(li.index());
});
Onur Topal
  • 3,042
  • 1
  • 24
  • 41