Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 917
dr3w Avatar asked Feb 26 '10 14:02

dr3w


People also ask

Can we get the children element from the parent element using jQuery?

jQuery children() method is used to get the direct children of the selected HTML element. You can use children() method to traverse through the child elements of the selected parent element.

How do you get children of children in jQuery?

Definition and Usage. The children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.

What is the use of parent () and child () method in jQuery?

It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.

How to find child elements using jQuery?

The main class is the parent element placed inside the <div> tag. The ul is child element and li is grandchild element of the parent element. Step 3: The jQuery find child syntax used in the web page. The find method placed in the script tag along with parent and child elements. The below sample is a combination of all steps.

How to get the position of an element relative to parent?

How to get the position of an element relative to the parent using jQuery. Answer: Use the jQuery position() method. You can easily find the position of an element relative to the offset parent using the jQuery position() method. It is only applicable for the visible elements.

How do I find the position of an element in jQuery?

Answer: Use the jQuery position () method You can easily find the position of an element relative to the offset parent using the jQuery position () method. It is only applicable for the visible elements. That means, you can get the position of elements with visibility: hidden; but not with display: none;.

What selectors can I add to the jQuery children() function?

Here is a list of a few selectors you can add to the .children () jQuery function: $ (":nth-child (n)"): detects the n th children of the indicated parent elements. $ (":nth-last-of-type"): detects the n th children of the indicated parent elements, in relation to siblings of the same element name. It counts from the last child to the first.


1 Answers

Use index():

$("#parentHldr > div").index($("#parentHldr > div.active"));
like image 111
Martin Avatar answered Nov 13 '22 01:11

Martin