[web] Javascript: Variable's Variable
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!
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.
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
Popular Topics
Advertisement