Element childElementCount Property - Javascript DOM

Javascript examples for DOM:Element childElementCount

Description

The childElementCount property returns how many child elements an element has.

The returned number contains the number of child element nodes, not the number of all child nodes, like text and comment nodes.

This property is read-only.

Return Value

A Number, representing the number of child elements of an element

The following code shows how to check how many child elements a <div> element has:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {//from w w  w.  ja v a  2 s .com
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>

<button onclick="myFunction()">find out how many children the div element below has</button>

<div id="myDIV">
  <p>First p element</p>
  <p>Second p element</p>
</div>

<p id="demo"></p>

<script>
function myFunction() {
    var c = document.getElementById("myDIV").childElementCount;
    document.getElementById("demo").innerHTML = c;
}
</script>

</body>
</html>

Related Tutorials