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?
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?
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>
).
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.
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.