tags:

views:

103

answers:

3

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

+2  A: 

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

window["myVar"]
Gabe Moothart
To elaborate: function setGlobal(varName, varValue) { window[varName] = varValue; }
Ates Goral
+5  A: 

Given:

var x = {
    myproperty: 'my value'
};

You can access the value by:

var value = x['myproperty'];

If you're looking for a global variable, then you would check its container (window);

var value = window['x']['myproperty'];
Ken Browning
+1  A: 

You can use eval(variableString); Readup on eval(); I am also new, so please proceed with caution as many don't recommend using eval();

Thanks, Mahesh Velaga.

Mahesh Velaga