Computer >> Computer tutorials >  >> Programming >> HTML

HTML DOM Storage setItem() method


The HTML DOM Storage setItem() method is used for removing a storage object item by passing a given key name.

Syntax

Following is the syntax for Storage removeItem() method −

localStorage.removeItem(keyname,value);

OR

sessionStorage.removeItem(keyname,value );

Here, keyname is of type string and represents the key name used for getting the value. The second parameter value represents the new value that will replace the old value.

Example

Let us look at the example for the Storage setItem() method −

<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">Storage setItem() method example</h1>
<p>Create the localstorage item by clicking the below button</p>
<button onclick="itemCreate()">CREATE</button>
<p>Display the localstorage item by clicking the below button</p>
<button onclick="itemShow()">DISPLAY</button>
<p id="Sample"></p>
<script>
   function itemCreate() {
      localStorage.setItem("TEXT1","HELLO WORLD");
      document.getElementById("Sample").innerHTML ="The key-value pair has been created";
   }
   function itemShow() {
      var s = localStorage.getItem("TEXT1");
      document.getElementById("Sample").innerHTML ="The 'TEXT1' key value is "+s;
   }
</script>
</body>
</html>

Output

This will produce the following output −

HTML DOM Storage setItem() method

On clicking the CREATE button −

HTML DOM Storage setItem() method

On clicking the DISPLAY button −

HTML DOM Storage setItem() method