javascript - Assembling a var dynamically -
this question has answer here:
how can assemble var in javascript during runtime without eval?
var lc = $('.bezeichnung-1').length; (var lt = 1; lt <= lc; lt++) { eval("var neuerwert"+lt+"=0;"); // works don't want use because read eval bad } var lc = $('.bezeichnung-1').length; (var lt = 1; lt <= lc; lt++) { window["var neuerwert"+lt] = 0; // not work }
how can assemble var in javascript during runtime without eval?
you don't, can make property of something.
if these @ global scope, they're properties:
var lc = $('.bezeichnung-1').length; (var lt = 1; lt <= lc; lt++) { window["neuerwert"+lt] = 0; // -----^ no `var` keyword }
if they're not @ global scope (good you!), make them properties of object, e.g.:
var neuerwert = { 1: /*...value here...*/, 2: /*....value here...*/ };
or array
var neuerwert = [ /*...value here...*/, /*....value here...*/ ];
and then
var lc = $('.bezeichnung-1').length; (var lt = 1; lt <= lc; lt++) { neuerwert[lt] = 0; }
note array indexes start @ 0
, may have adjust lt
if you're using array.
Comments
Post a Comment