Javascript DOM HTML NodeList length Property get children count for <body>

Introduction

Return the number of child nodes of the <body> element:

var nodelist = document.body.childNodes.length;

Click the button get the node names of the body element's child nodes.

Whitespace inside elements is considered as text, and text is considered as nodes. Comments are also considered as nodes.

View in separate window

<!DOCTYPE html>
<html>
<body><!-- This is a comment node! -->


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

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

<script>
function myFunction() {/*from  ww w  . j  ava  2s.  com*/
  var nodelist = document.body.childNodes.length;
  document.getElementById("demo").innerHTML = nodelist;
}
</script>

</body>
</html>



PreviousNext

Related