Open In App
Related Articles

HTML | DOM Storage removeItem() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

The DOM Storage removeItem() Method is related to the Storage object and is used to remove the specified storage object item. Storage object can be either a localStorage or sessionStorage Object.

Syntax: 

  • Local Storage Remove Item: 
localStorage.key(keyname)
  • Session Storage Remove Item: 
sessionStorage.key(keyname)

Parameters: It accepts a required parameter keyname i.e. name of the item to the removed.

Return Values: It doesn’t return any value.

Below is the HTML code to show the working of HTML DOM Storage removeItem() Object: 

Example 1: 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM Storage removeItem() Method
    </title>
        <!-- Script to get the name of the key -->
    <script>
        function myGeeks() {
             
        // Storing key present at 0th index
        var key = localStorage.key(0);
         
        // Removing key at 0th index
        localStorage.removeItem(key);
         
        // Printing key at 0th index
        var key2 = localStorage.key(0);
                 
        document.getElementById("geeks").innerHTML =
        "Previous value at 0th index was " + key + "<br>"
        + "Current value at 0th index is " + key2;
        }
    </script>
</head>
 
<body>
    <h1>GeeksForGeeks</h1></b>
    <h2 >DOM Storage removeItem() Method</h2>
 
    <button onclick="myGeeks()">
        Submit
    </button>
 
    <p id="geeks"></p>
 
  
</body>
 
</html>                   


Output: 

  • Before Click: 

  • After Click: 

Below is the HTML code to show the working of HTML DOM Storage removeItem() Object: 

Example 2: Session item. 

HTML




<!DOCTYPE html>
<html>
 
<body>
 
    <h1 style="color: green;">
      Geeks for Geeks</h1>
 
    <h2>Storage removeItem() Method</h2>
 
     
 
<p>Display Session Item</p>
 
 
 
    <button onclick="displayItem()">Display</button>
 
     
 
<p>Delete Session Item:</p>
 
 
 
    <button onclick="deleteItem()">Delete</button>
 
    <p id="try"></p>
 
 
 
    <script>
        // set item.
        sessionStorage.setItem(
          "gfg", "I am created");
 
        function deleteItem() {
            // Remove item.
            sessionStorage.removeItem("gfg");
        }
 
        function displayItem() {
            //  Display item.
            var remove_item =
                sessionStorage.getItem("gfg");
           
            document.getElementById(
              "try").innerHTML = remove_item;
        }
    </script>
</body>
 
</html>


Output: 

  • Before Click: 

  • After Click: 

Supported Browsers: The browser supported by DOM Storage removeItem() are listed below: 

  • Google Chrome 4 and above
  • Edge 12 and above
  • Internet Explorer 8 and above
  • Firefox 3.5 and above
  • Opera 10.5 and above
  • Safari 4 and above

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 : 04 Aug, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials