By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,379 Members | 1,748 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,379 IT Pros & Developers. It's quick & easy.

string -> variable name

P: n/a
How do I - within in a function - make Javascript recognize a string as a
variable name?

Thanks and cheers,
Konrad
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Konrad Mathieu wrote on 05 jul 2003 in comp.lang.javascript:
How do I - within in a function - make Javascript recognize a string as a
variable name?


<script>
var a="blah"
var blah="bloob"
var blib="bloobbloob"

function f(){
alert(window[a])
alert(window["blib"])
}

f() // alerts: bloob, bloobbloob

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #2

P: n/a
In article <10****************@iris.uk.clara.net>, "Konrad Mathieu"
<no****@nospam.com> writes:
How do I - within in a function - make Javascript recognize a string as a
variable name?

Thanks and cheers,
Konrad


How you do that depends on what the string's variable representation
represents.

For the most part, you simply use bracket notation.

If the string points at a variable, window['StringHere'] will give you a
reference to global variables (there are some cavaets to it I believe).

If its a name of a form element:

document.forms[varName].elements[varName2].propertyName
--
Randy
All code posted is dependent upon the viewing browser
supporting the methods called, and Javascript being enabled.
Jul 20 '05 #3

P: n/a
Thank you guys! This helped me a great deal.

Cheers,
Konrad

"HikksNotAtHome" <hi************@aol.com> schrieb im Newsbeitrag
news:20***************************@mb-m25.aol.com...
In article <10****************@iris.uk.clara.net>, "Konrad Mathieu"
<no****@nospam.com> writes:
How do I - within in a function - make Javascript recognize a string as a
variable name?

Thanks and cheers,
Konrad


How you do that depends on what the string's variable representation
represents.

For the most part, you simply use bracket notation.

If the string points at a variable, window['StringHere'] will give you a
reference to global variables (there are some cavaets to it I believe).

If its a name of a form element:

document.forms[varName].elements[varName2].propertyName
--
Randy
All code posted is dependent upon the viewing browser
supporting the methods called, and Javascript being enabled.

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.