How to change a single value inside a localStorage element?

I am trying to change the value inside localstorage. This item is the status of the checkbox. I want every time a checkbox is set to set the flag to true or false. I tried many ways until I realized that you cannot change the value without using JSON.

To add the value I'm using:

localStorage.setItem("status-" + i, $status.is(":checked")); 

and for uninstall I use:

 var parentId = $this.parent().attr('id'); localStorage.removeItem("'" + parentId + "'"); 

Now, to change the value I tried:

 $itemList.delegate("#status-" + i, 'click', function(e) { var $this = $(this); var parentId = this.parent().attr('id'); if ($this.is(":checked")) { localStorage.setItem("'" + parentId + "'".val("fdgsdagf")); // Note that alert here works. } }); 

Here's what my local storage looks like: Hope someone can help me. I worked on it for several days ...

thanks a lot

+4
source share
2 answers

setItem takes two parameters:

 localStorage.setItem('status-1', "'" + parentId + "'".val("fdgsdagf")); 

or more likely for your case:

 localStorage.setItem(parentId, "fdgsdagf"); 

Best practics:

 localStorage.setItem('key', JSON.stringify(value)); JSON.parse(localStorage.getItem('key')); 

Here is an example: http://jsfiddle.net/F8sF2/

EDIT: from you violin you need to change:

 var parentId = this.parent().attr('id'); 

to

 var parentId = $this.attr('id'); 

UPDATED http://jsfiddle.net/CC5Vw/1/

+4
source

try it

 localStorage.setItem(parentId, "fdgsdagf"); 

localStorage is nothing but a JavaScript object, you can think of them as an associated array. Therefore, you can use them like this.

To set a value

 localStorage[parentId] = "fdgsdagf"; 

To get the value

 var status = localStorage[parentId]; 
+3
source

All Articles