Can I set a variable to a variable combination in javascript? -
this question has answer here:
- access javascript variables dynamically 4 answers
here's code:
var album1 = "first album name"; var album2 = "second album name"; var album3 = "third album name"; var album4 = "fourth album name"; var album5 = "fifth album name"; var currentalbumctr = 1; var currentalbum = album + currentalbumctr;
currentalbum should set album1 if currentalbumctr 1. can help?
you use eval
achieve want, wrong way it. why not use array:
var album = []; album[1] = "first album name"; album[2] = "second album name"; album[3] = "third album name"; album[4] = "fourth album name"; album[5] = "fifth album name"; currentalbumctr = 1; currentalbum = album[currentalbumctr];
or, alternatively,
var album = { "1": "first album name"; "2": "second album name"; "3": "third album name"; "4": "fourth album name"; "5": "fifth album name"; };
or even
var album = [ "first album name", "second album name", "third album name", "fourth album name", "fifth album name" ]; currentalbumctr = 1; currentalbum = album[currentalbumctr-1];
note -1
on last statement since arrays indexed 0
Comments
Post a Comment