dr3w
dr3w

Reputation: 996

How to find position number of a certain child element in a parent element using jQuery

I have a div containing several other divs containing an image. It looks smth like this

<div id="parentHldr">
 <div class="imgHldr"><img src="foo/bar.png" id="1"></div>
 <div class="imgHldr"><img src="foo/bar.png" id="2"></div>
 <div class="imgHldr"><img src="foo/bar.png" id="3"></div>
 <div class="imgHldr active"<img src="foo/bar.png" id="4"></div>
 <div class="imgHldr"><img src="foo/bar.png" id="5"></div>
 <div class="imgHldr"><img src="foo/bar.png" id="6"></div>
</div>

I want to know the position of div that has class active. I get total number of children elements with this thing

$('#parentHldr').children().length

So i presume, there should be a way of finding the positional number of that div somehow...

ok, i've practically found the solution. Now its a bit more complicated. I need to get index of a DIV w/ class imgHldr containing img with id 5 inside the parent DIV w/ class parentHldr. Is this possible??))

Upvotes: 11

Views: 7511

Answers (1)

Martin
Martin

Reputation: 38289

Use index():

$("#parentHldr > div").index($("#parentHldr > div.active"));

Upvotes: 19

Related Questions