HTMLCollection length Property

The length() Property is used to return the collection of all HTML elements in a document. It is read-only property and is quite useful when a user wants to loop through an HTML Collection.

Syntax:

HTMLCollection.length 

Return Value: It returns a number which represent the number of all elements in a HTML Collection.

Below example shows how to count the all <p> element in the html collection:
Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1>GeeksForGeeks</h1>
        <h2>
          HTMLCollection length Property
        </h2>
  
        <p>
          The length property is used to return the 
          number of all elements in a HTMLCollection.
        </p>
        <div>
            <p>GeeksForGeeks</p>
            <p>A Computer science portal for geeks</p>
        </div>
  
        <button onclick="Geeks()">Submit</button>
  
        <!-- l is the HTML collection here -->
        <script>
            function Geeks() {
                var l = 
                    document.getElementsByTagName("P").length;
                alert(l);
            }
        </script>
  </center>
  
</body>
  
</html>

chevron_right


Output:
Before clicking on the button:

After clicking on the button:

Example-2: To highlight all the elements with class GFG using length property.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1>GeeksForGeeks</h1>
        <h2>HTMLCollection length Property</h2>
  
        <p>
          The length property is used to return the 
          number of all elements in a HTMLCollection.
         </p>
        <div>
            <p class="GFG">GeeksForGeeks</p>
            <p class="GFG">
              A Computer science portal for geeks
            </p>
        </div>
  
        <button onclick="Geeks()">Submit</button>
  
        <!-- w is the HTML collection here -->
        <script>
            function Geeks() {
                var w, c;
  
                w = document.getElementsByClassName("GFG");
                for (c = 0; c < w.length; c++) {
                    w[c].style.backgroundColor = "BLUE";
                }
            }
        </script>
  </center>
  
</body>
  
</html>

chevron_right


Output:
Before clicking on the button:

After clicking on the button:

Supported Browsers: The browser supported by HTMLCollection item() Method are listed below:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.