[web] Javascript: Variable's Variable

Started by
2 comments, last by Fuzztrek 18 years, 8 months ago
I have no idea on how to put a variable in a variable's name, tried something like var1+var2 = 'something' and some other combinations. Please help!
Current Project: Espion Online
Advertisement
eval() might be what you're looking for - it allows you to execute a string as if it was a piece of JavaScript. For example:
var test_var = 'Hello'; // Our test variablevar var_name = 'test_var'; // Set var_name to equal the name of our test variableeval(var_name+"='Goodbye'"); // "eval()" some code that changes the value in our test variablealert(test_var);
displays a message box that says 'Goodbye' on it.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Thanks! Just did eval("pInfo"+part_num+"='hi'"); and it worked perfectly!
Current Project: Espion Online
Eval is pretty inefficient, though, so if you can find any other way to do what you want, use that instead. I'm not sure of the context of your code, but you could probably get by using an array (where the key is the part number).

This topic is closed to new replies.

Advertisement