Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to access a javascript variable using a string that contains the name of the variable?

Tags:

javascript

This way I could have a function that says whatever_way_you_do_this = something. Is this possible? Basically I could tell a function which variable I want to set by giving it a string that holds the name of the variable.

Thanks

like image 700
MetaGuru Avatar asked Sep 17 '09 21:09

MetaGuru


2 Answers

If it is a global variable named myVar, you can use:

window["myVar"]
like image 143
Gabe Moothart Avatar answered Sep 17 '22 20:09

Gabe Moothart


You can use

eval(variableString);

Proceed with caution as many don't recommend using eval()

like image 30
Mahesh Velaga Avatar answered Sep 20 '22 20:09

Mahesh Velaga