Element childNodes Property - Find out how many child nodes a <div> element has: - Javascript DOM

Javascript examples for DOM:Element childNodes

Description

Element childNodes Property - Find out how many child nodes a <div> element has:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {/*from   w w  w.ja  v a2s  . c  om*/
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>

<button onclick="myFunction()">Test</button>

<div id="myDIV">
  <p>First p element (index 1)</p>
  <p>Second p element (index 3)</p>
</div>

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

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

</body>
</html>

Related Tutorials