Reputation: 729
How can I store multiple data in localStorage
? Using an array doesn't seem to work.
for(i=0;i<8;i++){
localStorage.vals[i]= item[i]; //vals[i] does not work
}
Upvotes: 2
Views: 55
Reputation: 597
To add to this answer there are lot of custom created storages for HTML5 available in GitHub:
You can go through the below mentioned article for the complete understanding of localStorage in HTML5:
http://www.smashingmagazine.com/2010/10/local-storage-and-how-to-use-it/
Upvotes: 0
Reputation: 87203
Use setItem()
and getItem()
methods of localStorage
.
Set the Object
in localStorage
.
var data = {};
for (i = 0; i < 8; i++) {
data[vals[i]] = item[i];
}
localStorage.setItem('myData', JSON.stringify(data));
To get
it:
var myData = JSON.parse(localStorage.getItem('myData'));
Update
To check if data
already exists in localStorage
:
if (localStorage.getItem('myData')) {
// Already Exists
}
Upvotes: 4
Reputation: 1407
LocalStorage can only store string data.
You could store your arrays and objects in JSON format though:
localStorage.vals = JSON.stringify(item);
Upvotes: 3