vishnu
vishnu

Reputation: 4599

update the value of the json key using HTML5 localstorage

var data = {  
       "mid":"10000XXX",          
       "css":{  
          "header":{  
             "background-color":"#000",
             "color":"#fff",
             "font-size":"10"
          },
          "txndescr":{  
             "background-color":"#000",
             "color":"#fff",
             "font-size":"10"
          }          
       }
    };

I have stored in the localstrorage in the following way,

// Put the object into storage
localStorage.setItem('defaultTheme', JSON.stringify(data));

And i want to update the 'header' background-color (#000 to #FFF) property. how to update the value.

Upvotes: 0

Views: 357

Answers (1)

Dennis Jaamann
Dennis Jaamann

Reputation: 3565

The way to update an Object already in localStorage is as follows:

  1. Fetch the existing item
  2. Update the value
  3. Save the updated item

    //1. Fetch the existing item
    var data = JSON.parse(localStorage.getItem('defaultTheme'));
    //2. Update the value
    data.css.header['background-color'] = '#CCC';
    //3. Save the update item
    localStorage.setItem('defaultTheme', JSON.stringify(data));
    

Cheers

Upvotes: 2

Related Questions