Element NodeList Property - Loop through the child nodes of <body> and output the node name of each child node: - Javascript DOM

Javascript examples for DOM:Element NodeList

Description

Element NodeList Property - Loop through the child nodes of <body> and output the node name of each child node:

Demo Code

ResultView the demo in separate window

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

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

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

<script>
function myFunction() {// w w w  .j a v  a  2 s  .co  m
    var nodelist = document.body.childNodes;

    var txt = "";
    var i;
    for (i = 0; i < nodelist.length; i++) {
        txt = txt + nodelist[i].nodeName + "<br>";
    }

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

</body>
</html>

Related Tutorials