HTMLCollection item() Method

The item() method is used to return the content of element at the given index of the collection of all HTML element. The index starts from zero(0). The elements in the collection are sorted in the order as they appear in the sourcecode.

Syntax:

HTMLCollection.item(index) 

OR



HTMLCollection[index] 

Parameters: It contains a number that represents the index of the element that the user wants to return. The index starts from 0.

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 item() Method</h2>
  
        <button onclick="Geeks()">Submit</button>
  
        <script>
            function Geeks() {
                var w = document.getElementsByTagName("h1");
                alert(w.item(0).innerHTML);
            }
        </script>
  
</body>
  
</html>

chevron_right


Output:
Before clicking on the button:

After clicking on the button:

Example-2: To change the content of an HTML Element.

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 item() Method</h2>
        <p>geeforgeefs</p>
        <button onclick="Geeks()">Submit</button>
  
        <script>
            function Geeks() {
                document.getElementsByTagName("P")[0].innerHTML = 
                                                  "GeeksForGeeks";
            }
        </script>
  
</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 :


1


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