473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Passing Variables Between Functions

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
1 2177
acoder
16,027 Expert Mod 8TB
Just define the variable outside the function, then you can set it within the function and use it outside the function.
Apr 9 '07 #2

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
3
by: jeffg | last post by:
The answer probably is staring me in the face and I don't see it, so I'll just ask. I try to limit the scope of variables and avoid global variables. But sometimes I get stumped by the following...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
3
by: tigrfire | last post by:
I don't want to use global variables to do this, but I'm stumped on how to pass a variable between one function to another, so if someone could help out, that'd be appreciated. Here's an example of...
3
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford...
7
by: Khai | last post by:
First off, yes, I understand the crapload of tutorials out there, (well, rather, I understand there /are/ a crapload of tutorials out there), the problem is my comprehension. I'm trying to pass...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.