+ 15

How I can find number of child elements present in parent element [ Solved ]

<div class="parent"> <span></span> <span></span> <span></span> <span></span> </div> 🔥 Tell me how can I find no of child present in div.parent element

6th Jun 2020, 5:46 PM
DIVYANSHU 😸
DIVYANSHU 😸 - avatar
5 ответов
+ 6
There are various ways to know about child elements . HERE ARE SOME OF THEM. var p = document.querySelector('.parent') 1. p.childElementCount 2. p.children.length 3. Interesting way var childs = document.querySelectorAll('.parent > *'); childs.length // gives you total number of children PS: I suggest you go to dev tools of browser and console log the parent you can find various properties related to it. Happy Coding. 😊
7th Jun 2020, 11:20 AM
Jaseem Akhtar
+ 2
document.querySelector(".parent").children.length //4
6th Jun 2020, 5:53 PM
viknesh vikky
viknesh vikky - avatar
+ 2
This can be finded e.g.: let span_0 = document. getElementById("mySpans").[0];
6th Jun 2020, 7:11 PM
JaScript
JaScript - avatar
+ 2
Here are two ways 1.first Of all document.querySelector(".parent").children.length It will return 4 2.use it: Document.getElementsByTagName("span")[1]; (you can also access 2 span if you give an id to it)
7th Jun 2020, 12:43 AM
Coder
Coder - avatar
+ 1
document.querySelectorAll(“.parent > *”).length; this selects all elements that are a direct descendant of .parent
8th Jun 2020, 7:42 AM
Rybka
Rybka - avatar