Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 localStorage getting key from value [closed]

localStorage.getItem will retrive value.

and setItem will set value.

But If I want to know which key was associated with this particular value? Then how to get Key of that.?

so question I have reffered

like image 204
V.J. Avatar asked Oct 18 '12 07:10

V.J.


People also ask

How do I know if my localStorage contains a key?

To check if a key exists or not in localStorage, we can use the localStorage. getItem() method. The localStorage. getItem() method takes the key as an argument and returns the key's value.

Does localStorage clear on browser close?

localStorage is similar to sessionStorage , except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends — that is, when the page is closed.


1 Answers

visit Html5 Storage Doc to get more details. Use the following syntax to set and get the values in localstorage/sessionstorage for storing values for a session

sessionStorage.getItem('key')
sessionStorage.setItem('key','value')

or store values permanently using

localStorage.getItem('key')
localStorage.setItem('key','value')

and u said u want to know the value but you want key then you can use the function

  localStorage.key(i);

or also you can loop through the available keys and get the desired key by cross checking the value

for(var i=0, len=localStorage.length; i<len; i++) {
    var key = localStorage.key(i);
    var value = localStorage[key];
    if(value.equals(desired_value))
    console.log(key + " => " + value);
}
like image 127
Neji Avatar answered Sep 19 '22 06:09

Neji