Javascript DOM HTML Node nodeType Property for all children

Introduction

Get the node types of the <body> element's child nodes:

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

Whitespace inside elements is considered as text, and text is considered as nodes.

Comments in the document are considered as comment nodes.

View in separate window

<!DOCTYPE html>
<html>
<body>

<p>test</p>

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

<!-- My personal comment goes here..  -->


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

<script>
function myFunction() {/*w w w  . ja  v a  2  s. c  o m*/
  var c = document.body.childNodes;
  var txt = "";
  var i;
  for (i = 0; i < c.length; i++) {
    txt = txt + c[i].nodeType + "<br>";
  }

  document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>



PreviousNext

Related