Open In App

HTML DOM length Property

The DOM length property in HTML is used to get the number of items in a NodeList object. A NodeList is a collection of child Nodes. For example, the NodeList of the body will contain all the child nodes in the body i.e. paragraphs, comments, headings, script, etc.

Syntax:  



nodelist.length

Return Value: It returns a numeric value that represents the number of nodes in the nodelist. 

Example: The below program illustrates the DOM length property in HTML:






<!DOCTYPE html>
<html>
<head>
    <title>
        DOM Length Property
    </title>
    <script>
        function geeksFunction() {
            let node_list = document.body.childNodes;
            let elements = "";
            let i;
            for (i = 0; i < node_list.length; i++) {
                elements = elements + node_list[i].nodeName
                    + "    ";
            }
            elements = elements + "<br> Length: "
                + document.body.childNodes.length;
            document.getElementById("demo").innerHTML =
                elements;
        }
    </script>
</head>
<body style="text-align:center">
    <h1>GeeksforGeeks</h1>
    <h2>DOM Length Property</h2>
    <button onclick="geeksFunction()">Click Here!</button>
    <p id="demo"></p>
</body>
</html>

Output:

 

Supported Browsers: The browser supported by DOM length property are listed below: 


Article Tags :