javascript - Trying to set dynamic JS variables in a function globally -


i'm trying set local storage values using dynamic variables within function looped through. i'm trying (which works isn't dynamic):

 localstorage.lvlzerovalue = localstorage.lvlzeromaxvalue; 

using this:

countermarkers[numberid] = maxmarkers[numberid]; 

but it's not affecting 'localstorage.lvlzerovalue' @ global level

$('#spellcounttackmax').click(function() {      var countermarkers = [         localstorage.lvlzerovalue,         localstorage.lvlonevalue,         localstorage.lvltwovalue,         localstorage.lvlthreevalue,         localstorage.lvlfourvalue,         localstorage.lvlfivevalue,         localstorage.lvlsixvalue,         localstorage.lvlsevenvalue,         localstorage.lvleightvalue,         localstorage.lvlninevalue     ];      var maxmarkers = [         localstorage.lvlzeromaxvalue,         localstorage.lvlonemaxvalue,         localstorage.lvltwomaxvalue,         localstorage.lvlthreemaxvalue,         localstorage.lvlfourmaxvalue,         localstorage.lvlfivemaxvalue,         localstorage.lvlsixmaxvalue,         localstorage.lvlsevenmaxvalue,         localstorage.lvleightmaxvalue,         localstorage.lvlninemaxvalue     ];          jquery.fn.ontackset = function(numberid){              return this.each(function(){                  if(maxmarkers[numberid] == "" || maxmarkers[numberid] == null || maxmarkers[numberid] == 0 ) {                      alert("not else ran");                     $(this).attr('value', "0");                     $('#spin' + numberid).attr('value', "0");                     countermarkers[numberid] = "0";                     maxmarkers[numberid] = "0";                    } else {                     alert("else ran");                     $(this).attr('value', maxmarkers[numberid]);                     $(this).attr('max', maxmarkers[numberid]);                     // localstorage.lvlzerovalue = localstorage.lvlzeromaxvalue;                     alert(countermarkers[numberid]);                     alert(maxmarkers[numberid]);                      // works isn't dynamic                     localstorage.lvlzerovalue = localstorage.lvlzeromaxvalue;                      // attempt @ making dynamic doesn't seem work globally                     countermarkers[numberid] = maxmarkers[numberid];                  }             });         };          $("#spin0").ontackset(0); 

so i'm pretty sure issue scope, yet can't seem right. please, help. thanks!

do need keep keys storage values way have them? if change them lvlzerovalue lvl0value use approach below:

    jquery.fn.ontackset = function(numberid){              return this.each(function(){                  if(localstorage['lvl'+numberid+'maxvalue'] == "" || localstorage['lvl'+numberid+'maxvalue'] == null || localstorage['lvl'+numberid+'maxvalue'] == 0 ) {                      alert("not else ran");                     $(this).attr('value', "0");                     $('#spin' + numberid).attr('value', "0");                     localstorage['lvl'+numberid+'value'] = "0";                     localstorage['lvl'+numberid+'maxvalue'] = "0";                    } else {                     alert("else ran");                     $(this).attr('value', localstorage['lvl'+numberid+'maxvalue']);                     $(this).attr('max', localstorage['lvl'+numberid+'maxvalue']);                      localstorage['lvl'+numberid+'value'] = localstorage['lvl'+numberid+'maxvalue'];                  }             });         }; 

if need keep keys have still adapt above approach needs building arrays different:

var markers = [         'lvlzerovalue',         'lvlonevalue',         'lvltwovalue',         'lvlthreevalue',         'lvlfourvalue',         'lvlfivevalue',         'lvlsixvalue',         'lvlsevenvalue',         'lvleightvalue',         'lvlninevalue'     ]; var maxmarkers = [         'lvlzeromaxvalue',         'lvlonemaxvalue',         'lvltwomaxvalue',         'lvlthreemaxvalue',         'lvlfourmaxvalue',         'lvlfivemaxvalue',         'lvlsixmaxvalue',         'lvlsevenmaxvalue',         'lvleightmaxvalue',         'lvlninemaxvalue'     ]; 

and use them this:

localstorage[markers[numberid]] = localstorage[maxmarkers[numberid]]; 

here fiddle see how works.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -