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

Javascript examples for DOM:Element children

Description

Element children Property - Find out how many children 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. c  o m
    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 (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>

Related Tutorials