javascript - Use i in var name on a for cicle -


this not working have no idea how correct it:

for(i=1; i<=3; i++){     var address_con+[i] = document.getelementbyid("address_con[i]")     var cap_con+[i] = document.getelementbyid("cap_con[i]")     var city_con+[i] = document.getelementbyid("city_con[i]") } 

it should produce:

//first element address_con1 = document.getelementbyid("address_con1") etc. //last element city_con3 = document.getelementbyid("city_con3") 

the error on:

var name+[i] 

thank you

there no replacement of variables inside strings in javascript (unlike in php, possible). similar creating new variables, can't in way, trying.

to naming, can use following code, store values inside result variable.

var result = {}; for(var i=1; i<=3; i++){     result[ "address_con" + ] = document.getelementbyid("address_con" + i)     result[ "cap_con" + ] = document.getelementbyid("cap_con" + i)     result[ "city_con" + ] = document.getelementbyid("city_con" + i) } 

Popular posts from this blog

Php - Delimiter must not be alphanumeric or backslash -

c# - How to change the "Applies To" field under folder auditing options programatically (.NET) -

c++ - Ambiguity when using boost::assign::list_of to construct a std::vector -