Passing a variable outside of a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satterfieldben
    New Member
    • Apr 2007
    • 3

    Passing a variable outside of a function

    I have a newbie question about passing variables between functions. I am wanting to use a drop down box to select a value. Then base on which was selected, it would create a variable and I would call that variable in another Java script.

    Sample script
    <SCRIPT LANGUAGE="JavaS cript">
    function GetSelectedItem () {

    len = document.formna me.selectname.l ength
    i = 0
    chosen = "none"

    for (i = 0; i < len; i++) {
    if (document.formn ame.selectname[i].selected) {
    chosen = document.formna me.selectname[i].value
    }
    }

    //return chosen
    alert(chosen);
    }
    </script>

    I tried setting a global variable to equal "chosen" but when I run the page it tells me that global variable is undefined. So my question is how do I define a local variable to be equal to a global variable, so that when I leave the function "chosen" still has a value.

    My best guess is the following script.
    <SCRIPT LANGUAGE="JavaS cript">
    function GetSelectedItem () {

    len = document.zonepi ck.zonebox.leng th
    i = 0
    chosen = "none"

    for (i = 0; i < len; i++) {
    if (document.zonep ick.zonebox[i].selected) {
    chosen = document.zonepi ck.zonebox[i].value;
    _global.thezone = chosen;
    }
    }

    //return chosen
    //alert(chosen);
    }

    alert(thezone);
    </script>

    But sadly that scripts doesn't work. Any help would be greatly appreciated, even if you just give me a google phrase so that I can search the answer myself.

    Ben
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You might want to try asking this in the Javascript forum - this is the Java forum.

    Comment

    • satterfieldben
      New Member
      • Apr 2007
      • 3

      #3
      That would make me a newbie...thanks for the heads up.

      Comment

      Working...