string -> variable name

K

Konrad Mathieu

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

Thanks and cheers,
Konrad
 
E

Evertjan.

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>
 
K

Konrad Mathieu

Thank you guys! This helped me a great deal.

Cheers,
Konrad

HikksNotAtHome said:
"Konrad Mathieu" said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top