Find out how many children a <div> element has:
var c = document.getElementById("myDIV").children.length;
Click the button to find out how many children the div element below has.
<!DOCTYPE html> <html> <head> <style> div {/*w w w .java 2 s. c o m*/ border: 1px solid black; margin: 5px; } </style> </head> <body> <button onclick="myFunction()">Test</button> <div id="myDIV"> <p>First p element (index 0)</p> <p>Second p element (index 1)</p> </div> <p id="demo"></p> <script> function myFunction() { var c = document.getElementById("myDIV").children.length; document.getElementById("demo").innerHTML = c; } </script> </body> </html>
The childNodes property contain all nodes, including text nodes and comment nodes.
The children property only contain element nodes.