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 body will contain all the child nodes in the body i.e. paragraph, comments, headings, script etc.

Syntax:

nodelist.value

Below program illustrates the DOM length property in HTML:



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>
            DOM Length Property
        </title>
          
        <script>
          
            function geeksFunction() { 
                  
                var node_list = document.body.childNodes;
                var elements = "";
                var 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>                    

chevron_right


Before Click on the button:

After click on the button:

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

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Appsecco | Web developer | IEEE | GeeksforGeeks Intern | Aspiring Cyber Security Specialist | Camcann Visit https//ashwingoelcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.