string -> variable name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Konrad Mathieu

    string -> variable name

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

    Thanks and cheers,
    Konrad


  • Evertjan.

    #2
    Re: string -> variable name

    Konrad Mathieu wrote on 05 jul 2003 in comp.lang.javas cript:[color=blue]
    > How do I - within in a function - make Javascript recognize a string as a
    > variable name?[/color]

    <script>
    var a="blah"
    var blah="bloob"
    var blib="bloobbloo b"

    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)

    Comment

    • HikksNotAtHome

      #3
      Re: string -&gt; variable name

      In article <1057428393.522 24.0@iris.uk.cl ara.net>, "Konrad Mathieu"
      <nospam@nospam. com> writes:
      [color=blue]
      >How do I - within in a function - make Javascript recognize a string as a
      >variable name?
      >
      >Thanks and cheers,
      >Konrad[/color]

      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.

      Comment

      • Konrad Mathieu

        #4
        Re: string -&gt; variable name -&gt; Thanks!

        Thank you guys! This helped me a great deal.

        Cheers,
        Konrad

        "HikksNotAtHome " <hikksnotathome @aol.com> schrieb im Newsbeitrag
        news:2003070514 4428.08200.0000 0613@mb-m25.aol.com...[color=blue]
        > In article <1057428393.522 24.0@iris.uk.cl ara.net>, "Konrad Mathieu"
        > <nospam@nospam. com> writes:
        >[color=green]
        > >How do I - within in a function - make Javascript recognize a string as a
        > >variable name?
        > >
        > >Thanks and cheers,
        > >Konrad[/color]
        >
        > 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.[/color]


        Comment

        Working...