Javascript: Using A String As A Variable
Javascript February 22nd, 2009I have a function that I send a javascript array to like so:
if (VAR_clicked == "one") {
FUN_Table(VAR_array_one);
} else if (VAR_clicked == "two") {
FUN_Table(VAR_array_two);
} else if (VAR_clicked =="etc") {
FUN_Table(VAR_array_etc);
}
My problem now is I have many more arrays and want to do something like this:
FUN_Table("VAR_array_" + VAR_clicked);
I’m trying to use a string to pass a variable to the function, this does not work.
Any ideas as to how I can do this?
You need to use
eval()
to turn your string to a variable.
FUN_Table( eval("VAR_array_" + VAR_clicked) );
And then your string will become your “variable”.