Thursday, 15 May 2014

html5 - Javascript help setting/getting local storage data for each input element -



html5 - Javascript help setting/getting local storage data for each input element -

i've been working on this, , it's working. have feeling setinterval within loop can't done, or isn't working. without setinterval , final 'if' statement, loops through elements , adds classname each if set to. here's script if can advise going wrong:

(function() { var localstorageid = document.getelementbyid('local-storage'); var inputtags = ['input', 'textarea', 'select', 'button']; // loop through input tags on page for(var = 0; < inputtags.length; i++) { // create variable matches input tags within our #localstorage var localstoragetag = localstorageid.getelementsbytagname(inputtags[i]); var formdata = {}; for(var z = 0; z < localstoragetag.length; z++) { formdata[localstoragetag[z].name] = localstoragetag[z].value; } localstorage.setitem('formdata', formdata); if(localstorage.getitem('formdata')) { // seek accomplish } } })();

you cannot store objects in localstorage. must strings. convert object string using json.stringify(), json.parse() string when retrieving localstorage.

edit: example:

localstorage.setitem('formdata', json.stringify(formdata)); var fd= json.parse(localstorage.getitem('formdata')); if(fd) { // seek accomplish }

javascript html5 local-storage

No comments:

Post a Comment