chromedude
chromedude

Reputation: 4302

In HTML5 client-side storage how do you get the value of the key?

I stored a value with a key like this localStorage[title] = text; I know I can recall the text by doing var text = localStorage[title] but how do you get the value of the title to insert into the localStorage so that the program knows what value to get. Is there any way to loop through the keys in the localStorage?

Upvotes: 0

Views: 350

Answers (1)

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827256

[removed for-in example]

The localStorage API, allows you to iterate over the keys, we have a length property, and the key function.

The key function takes an index and returns the name of the key:

var key, value;
for (var i = 0; i < localStorage.length; i++) {
  key = localStorage.key(i);
  value = localStorage.getItem(key);
  // use key or value
}

Try this example here.

Upvotes: 2

Related Questions