470,810 Members | 966 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

Passing a variable outside of a function

satterfieldben
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="JavaScript">
function GetSelectedItem() {

len = document.formname.selectname.length
i = 0
chosen = "none"

for (i = 0; i < len; i++) {
if (document.formname.selectname[i].selected) {
chosen = document.formname.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="JavaScript">
function GetSelectedItem() {

len = document.zonepick.zonebox.length
i = 0
chosen = "none"

for (i = 0; i < len; i++) {
if (document.zonepick.zonebox[i].selected) {
chosen = document.zonepick.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
Apr 5 '07 #1
2 1710
Ganon11
3,652 Expert 2GB
You might want to try asking this in the Javascript forum - this is the Java forum.
Apr 5 '07 #2
That would make me a newbie...thanks for the heads up.
Apr 5 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by kazack | last post: by
3 posts views Thread by domeceo | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.