HTML5 / JavaScript: save name and value of dynamic variable in localStorage

I would like to store the yes value in localStorage with movie1 key using javascript. However, I want the key part 1 be dynamic, based on what movieID set movieID .

This is what I have now, but it does not work:

 movieID = 1; localStorage.movie + movieID = 'yes'; 

Any thoughts or insights on how I can do this will be greatly appreciated. Thanks!

+7
source share
4 answers

Try the following:

 localStorage['movie'+movieID] = 'yes'; 
+14
source

You can use the setItem function to store values ​​in local storage:

 localStorage.setItem('movie' + movieID, 'yes'); 

Then, when you want to check the value, you can use

 localStorage.getItem('movie' + movieID); 
+11
source
+1
source

I think,

 localStorage.setItem('movie' + movieID, 'yes'); 

actually is an assistant in using the library and is not official.

0
source

All Articles