childElementCount in javascript

childElementCount property in javascript

childElementCount property is used to return the total count of the child elements within the element.

Syntax

var count = element.childElementCount;

Example 1 - Get total number of attributes present

Try Yourself

Example Explanation

  • In the above example we have created a div (idDiv), it is the parent element.
  • Within the parent element, we have added two child elements (div, p).

  • When the button is clicked fun_name Javascript function is called.
  • In the Javascript function, first we are getting the div (document.getElementById("idDiv")) and storing in attr variable.
  • Using the childElementCount property, we are getting the total number of child elements and displaying it (alert (attr.childElementCount)).