Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change a single value inside a localStorage item?

I'm trying to change a value inside localstorage. This item is the status of a checkbox. I want, everytime that a checkbox is checked to set the value to true or false of that checkbox. I tried many ways until I realized that there is no way you can change a value without using JSON.

To add the value I use:

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

and to delete 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.

                }
});

This is how my local storage looks like: I hope someone could help me. I've been working on it for few days...

Thanks alot

like image 526
jQuerybeast Avatar asked Sep 01 '11 00:09

jQuerybeast


People also ask

How do I edit localStorage?

Edit localStorage keys or values View the localStorage key-value pairs of a domain. Double-click a cell in the Key or Value column to edit that key or value.

How do I remove one element from local storage?

To delete local storage sessions, use the removeItem() method. When passed a key name, the removeItem() method removes that key from the storage if it exists. If there is no item associated with the given key, this method will do nothing.


1 Answers

setItem takes two parameters:

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

or more likely for your case:

localStorage.setItem(parentId, "fdgsdagf");

Best Practices:

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

Here's an example: http://jsfiddle.net/F8sF2/

EDIT: from you fiddle, you need to change:

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

to

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

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

like image 162
Joe Avatar answered Nov 02 '22 09:11

Joe