How to count amount of child elements of a parent class Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to count amount of child elements of a parent class without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have something like this.

function myFunction() {
  var c = document.getElementById("myID").childElementCount;
  alert(c);
}

Is there a way to do something like this?

function myFunction() {
  var c = document.getElementsByClass("myClass").childElementCount;
  alert(c);
}

Answer

There is no getElementByClass but there is getElementsByClassName. However, it returns a collection of elements because you can have multiple elements with the same class. IDs however are limited to one per document.

So if you knew the index of the element in the collection, you could do something like this:

var els = document.getElementsByClassName('myClass');
var c = els[0].childElementCount;
alert(c);
We are here to answer your question about How to count amount of child elements of a parent class - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji