Sunday 30 September 2018

JavaScript: childNodes: Get child nodes of an element

By using childNodes property of an element, you can get all the child nodes of an element.

Syntax
element.childNodes

childNodes.html
<!DOCTYPE html>

<html>

<head>
    <title>Child Nodes</title>

</head>

<body>
    <p class="color1" id="para1">
        Good Morning
    </p>

    <script>
        var element = document.getElementsByTagName("body")[0];

        var childs = element.childNodes;

        alert("Total child nodes " + childs.length);

        /*for(var i = 0; i < childs.length; i++){
         alert(childs[i].textContent);
        }*/
    </script>
</body>

</html>

Load above document in browser, you can able to see total child nodes of body element.

Previous                                                 Next                                                 Home

No comments:

Post a Comment