Javascript DOM HTML Node nodeName Property get children's node name

Introduction

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

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

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

View in separate window

<!DOCTYPE html>
<html>
<body>


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

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

<div><strong>Note:</strong> Comments in the document are considered as #comments, 
and comments are also considered as nodes.</div>

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

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

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

</body>
</html>



PreviousNext

Related