Open In App
Related Articles

HTMLCollection item() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

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: 

html




<!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>


Output: 

Before clicking on the button: 

 

After clicking on the button:

  

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

html




<!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>


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 1
  • Edge 12
  • Internet Explorer 8
  • Firefox 1
  • Opera 12.1
  • Safari 1

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 10 Aug, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials