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??))
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.
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.
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.
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 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.
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;.
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.
Use index():
$("#parentHldr > div").index($("#parentHldr > div.active"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With