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

Passing a variable outside of a function

satterfieldben
P: 3
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
Share this Question
Share on Google+
2 Replies

Ganon11
Expert 2.5K+
P: 3,652
You might want to try asking this in the Javascript forum - this is the Java forum.
Apr 5 '07 #2

satterfieldben
P: 3
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.