Get the name of the first local storage item:
var x = localStorage.key(0);
This example demonstrates how to use the key()
method to get the name of a local storage item.
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Get the name of the first local storage item</button> <p id="demo"></p> <script> function myFunction() {/*from ww w. j a v a 2s.co m*/ var x = localStorage.key(0); document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The key()
method returns name of the key with the specified index.
The key()
method belongs to the Storage Object, which can be either a localStorage object or a sessionStorrage object.
key(index);
Parameter Values
Parameter | Description |
---|---|
index | Required. A Number representing the index of the key you want to get the name of |