0

Has anyone got an idea how to get child's index in jQuery. E.g.:

<ul>
    <li>Item 1</li>
    <li class="active">Item 2</li>
    <li>Item 3</li>
</ul>

I want to get the li.active's child index ? Any ideas how?

SitecoreNoob
  • 301
  • 1
  • 3
  • 12
Yasen G.
  • 43
  • 6

4 Answers4

3

Assuming you mean you want to get the index, you can use jQuery's index() function, which returns the index relative to the parent as a 0-based integer. For example:

$('li.active').index();

In your code will produce 1 (because li.active is the second element of the parent <ul>).

jsFiddle Demo

From the Doc:

Return Values

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

BenM
  • 52,573
  • 26
  • 113
  • 168
1

You can use jQuery's index() function for this:

$( "li.active" ).index();

Return Values

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

Lix
  • 47,311
  • 12
  • 103
  • 131
1

You can use .index().

$('li.active').index();

Working Fiddle

Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
1
$("ul>li.active").index()

maybe what u want?

Lix
  • 47,311
  • 12
  • 103
  • 131
kingwrcy
  • 221
  • 2
  • 11