I'm trying to take a function's input (objectName) and make that value create a variable. So if objectName = "abcd" I would want the variable to be created be abcd. Is there a way I can do this in JavaScript?
1 Answer
You can use window[variableName] like this:
//Variable name
var name = "abc";
//window[name] is equal to the variable abc
window[name] = 123;
//show 123
alert(abc);
2 Comments
PugsOverDrugs
If you are using JQuery, the command window.name will create a global property of the window. This can be manipulated the same way a variable can. You would reference it with window.abc but, I like Shyrmes way a little bit better :D
JacksonML
Wow that was pretty simple haha. Thanks for the help. Worked like a charm in my function.