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

29,562

Solution 1

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/

Solution 2

Try this

localStorage.setItem(parentId, "fdgsdagf");

localStorage is nothing but JavaScript object you can treat them as associate array. So you can use them like this.

To set value

localStorage[parentId] = "fdgsdagf";

To get value

var status = localStorage[parentId];
Share:
29,562
jQuerybeast
Author by

jQuerybeast

A kid stepping an inch forward every day.

Updated on September 01, 2020

Comments

  • jQuerybeast
    jQuerybeast almost 4 years

    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

  • jQuerybeast
    jQuerybeast almost 13 years
    unfortunately none of both work. I tweaked them aswell in case anything similar works but yet again nothing works.
  • jQuerybeast
    jQuerybeast almost 13 years
    stackoverflow.com/questions/7207337/… On this post, a guy says you can't change the item's value without JSON. But the way he does it is abit confusing. Thanks
  • Joe
    Joe almost 13 years
    I think you would want $this.attr('id'), not parent as well
  • jQuerybeast
    jQuerybeast almost 13 years
    I think I will need to use the 'i' since I have "var i = Number(localStorage.getItem('todo-counter')) + 1," so that each time the checkbox increases value. Thank you
  • jQuerybeast
    jQuerybeast almost 13 years
    but it doesn't change the value of the status. Check: fiddle.jshell.net/CC5Vw/1/show with firebug.
  • Felipe Alarcon
    Felipe Alarcon over 8 years
    This doesn't really answer the question asked by the OP.