Sometimes, we want to get the child node count with JavaScript.
In this article, we’ll look at how to get the child node count with JavaScript.
How to get the child node count with JavaScript?
To get the child node count with JavaScript, we can use the childElementCount
property.
For instance, we write:
<ul>
<li>Array1</li>
<li>Array2</li>
<li id="element">Array3</li>
</ul>
to add a ul with some li elements.
Then we write:
const temp = document.getElementById('element').parentNode;
console.log(temp.childElementCount);
We select the li with ID element
with getElementById
.
Then we get the ul with parentNode
.
Finally, we get the number of li’s in the ul with childElementCount
.
Therefore, the console log should log 3.
Conclusion
To get the child node count with JavaScript, we can use the childElementCount
property.