javascript - Adding elements to object for localStorage -


i have dynamic listview displays various offers stores. want user able save these offers when select listview in order produce saved offers feature using localstorage.

i have managed reach point can save 1 offer selecting offer in listview.

on selection of listview row takes data attributes particular selection , populates array passed localstorage.

js

var offerobject = '';                                                                                                                                                                            $("#offers ul").on("click", ">li", function(event, ui) {                                                                                                                                                                                var offertitle = $(this).closest('li').attr('data-offer-title');      var offerdesc = $(this).closest('li').attr('data-offer-desc');     var offerexpiry = $(this).closest('li').attr('data-offer-expiry');       offerobject= {"offer":[          {              'offertitle': offertitle,              'offerdesc': offerdesc,             'offerexpiry': offerexpiry         }     ]};     localstorage.setitem('offerobject', json.stringify(offerobject)); }); 

i trying add offerobject variable when listview selection made in order produce array of saved offers.

currently if make selection offer replaced new selection of attributes list row.

from similar questions understand need use .push in order this? advice appreciated having difficulty doing so.

yes, typically done .push. steps follows:

//create offer var offer = {         'offertitle': offertitle,          'offerdesc': offerdesc,         'offerexpiry': offerexpiry }; // add offer array of offers , save var offers = json.parse(localstorage.getitem('offers')); // if offers null create empty array [] if (offers === null || offers === undefined) {     offers = []; } offers.push(offer); localstorage.setitem('offers', json.stringify(offers)); 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -