HTML | DOM Storage removeItem() Method
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 myBeginner() { // 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("Beginner").innerHTML = "Previous value at 0th index was " + key + "< br >" + "Current value at 0th index is " + key2; } </ script > </ head > < body > < h1 >w3wiki</ h1 ></ b > < h2 >DOM Storage removeItem() Method</ h2 > < button onclick = "myBeginner()" > Submit </ button > < p id = "Beginner" ></ 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;" > Beginner for Beginner</ 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