There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Web Design & Development
Tag Cloud
access acer asus bios bsod computer crash drive driver drivers error ethernet excel freeze games gaming graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard netgear network printer problem ram random registry router slow software sound trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless xbox
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Passing VAR from one function to another

Reply  
Thread Tools
madd74's Avatar
Computer Specs
Member with 267 posts.
 
Join Date: Nov 2003
Location: Des Moines, IA
Experience: Advanced
05-Nov-2008, 03:43 AM #1
Passing VAR from one function to another
I have been attempting to understand how to pass a varible from one function to another, and with no luck. Here is my code
Code:
<html>
<head>
<script type="text/javascript">
function check(x) {
  document.getElementById("yes").value=x;
  if (x=="a") {
    var stuff = "your mom";
  }
  else if (x=="b") {
    var stuff = "your dog";
  }
  alert(stuff);
  return stuff;
}
</script>
<script type="text/javascript">
function showme(stuff)
  {
  alert(stuff);
  }
</script>
</head>
<body>

<form>
<input type="radio" name="browser" onclick="check(this.value)" value="a">first<br />
<input type="radio" name="browser" onclick="check(this.value)" value="a">second<br />
<input type="radio" name="browser" onclick="check(this.value)" value="b">third<br />
<br />
<input type="text" id="yes" size="2"><br />
<input type="button" onclick="showme(stuff)" value="show" />
</form>

</body>
</html>
Now, clicking on first or second will output "a" to the text id of "yes" and an alert box will say "your mom". Clicking on third will output "b" to the text id of "yes" and an alert box will say "your dog". Clicking show will do nothing. If I replace the showme(stuff) with showme("text here") then "text here" will show up when I click the show button. My research in using radio buttons show they are extremely difficult to get info from as compared to about everything else in a form (go figure). I appreciate any assistance in advance!
__________________
madd says check out the always free and ad-less Madd's World - Red Mage of the Net (madd74.com) or check out my forum or enjoy my free arcade and beat my awesome high scores if you can
amanxman's Avatar
Senior Member with 690 posts.
 
Join Date: Mar 2006
Location: New Zealand
Experience: Advanced Web Developer
05-Nov-2008, 04:59 AM #2
Hey,

I'm by no way an expert on this kind of stuff, but initial thoughts are that when you're clicking show, it's going to show a variable dependant on what x is.

However, the variable 'stuff' is defined by the IFs BEFORE the X is defined.

In other works, think about it like this.

We run IF:
IF x = a, then say dog
IF x = b then say mum

We define x:
X = a

We ask it to say dog or mum

However, when the IF statement was run, X was no defined - X was defined AFTER the IF was run.

So,

We define x:
X = a

We run IF:
IF x = a, then say dog
IF x = b then say mum

We as it to say mum or dog. And it can because it knows what X is before the IF is run, and therefore it knows what to say.

Now, that's theoretically.

Practically I'm not sure sure. You might need to make the form submit itself, just parsing the variable (now defined) back into the IF statement.

Or, run the IF statement once it's been defined by the form, in the same session.

This might be total codswollop, and if it is, feel free to ignore me, but it does make half sense that the IF statement cannot be correctly working when X is not yet defined. If you run debug on the IF statement, you'll probably find it's coming out FALSE to both instances, i.e. x is not a and x is not b.

Perhaps the below will test this theory
Code:
function check(x) {
  document.getElementById("yes").value=x;
  if (x=="a") {
    var stuff = "your mom";
  }
  else {
    var stuff = "your dog";
  }
  alert(stuff);
  return stuff;
}
(note the second IF removed, and it is replaced with a simple ELSE)
If this shows your dog, it might indicate that your scripts are working ok, it's just that X is not defined when IF is running,

Let me know if i'm talking rubbish...!!!

Good luck
__________________
It deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht frist and lsat ltteer is at the rghit pclae. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe.
--------------
RB Web Works
MMJ's Avatar
MMJ MMJ is offline
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
05-Nov-2008, 09:31 AM #3
in the onclick where you have showme(stuff) stuff is not a valid variable because it is private to the check function only.

To fix this you must have it global:

HTML Code:
<html>
<head>
<script type="text/javascript">
var stuff = ''; //GLOBAL VARIABLE
function check(x) {
  document.getElementById("yes").value=x;
  if (x=="a") {
    stuff = "your mom";
  }
  else if (x=="b") {
    stuff = "your dog";
  }
  alert(stuff);
  return stuff;
}
</script>
<script type="text/javascript">
function showme(stuff)
  {
  alert(stuff);
  }
</script>
</head>
<body>

<form>
<input type="radio" name="browser" onclick="check(this.value)" value="a">first<br />
<input type="radio" name="browser" onclick="check(this.value)" value="a">second<br />
<input type="radio" name="browser" onclick="check(this.value)" value="b">third<br />
<br />
<input type="text" id="yes" size="2"><br />
<input type="button" onclick="showme(stuff)" value="show" />
</form>

</body>
</html> 
madd74's Avatar
Computer Specs
Member with 267 posts.
 
Join Date: Nov 2003
Location: Des Moines, IA
Experience: Advanced
05-Nov-2008, 04:13 PM #4
Quote:
Originally Posted by amanxman View Post
Hey,

I'm by no way an expert on this kind of stuff, but initial thoughts are that when you're clicking show, it's going to show a variable dependant on what x is.

However, the variable 'stuff' is defined by the IFs BEFORE the X is defined.

In other works, think about it like this.

We run IF:
IF x = a, then say dog
IF x = b then say mum

We define x:
X = a

We ask it to say dog or mum

However, when the IF statement was run, X was no defined - X was defined AFTER the IF was run.

So,

We define x:
X = a

We run IF:
IF x = a, then say dog
IF x = b then say mum

We as it to say mum or dog. And it can because it knows what X is before the IF is run, and therefore it knows what to say.

Now, that's theoretically.

Practically I'm not sure sure. You might need to make the form submit itself, just parsing the variable (now defined) back into the IF statement.

Or, run the IF statement once it's been defined by the form, in the same session.

This might be total codswollop, and if it is, feel free to ignore me, but it does make half sense that the IF statement cannot be correctly working when X is not yet defined. If you run debug on the IF statement, you'll probably find it's coming out FALSE to both instances, i.e. x is not a and x is not b.

Perhaps the below will test this theory
Code:
function check(x) {
  document.getElementById("yes").value=x;
  if (x=="a") {
    var stuff = "your mom";
  }
  else {
    var stuff = "your dog";
  }
  alert(stuff);
  return stuff;
}
(note the second IF removed, and it is replaced with a simple ELSE)
If this shows your dog, it might indicate that your scripts are working ok, it's just that X is not defined when IF is running,

Let me know if i'm talking rubbish...!!!

Good luck
Ignore? No way, I certainly appreciate the time you spend in looking into it for me Actually the part of the script you tackled is working okay. X is properly defined by the onclick event of the radio button. You click first, then a textarea shows either "a or "b", and that works. At the same time, the alert will display from the check(x) function. So, by all standards, troubleshooting shows that the check(x) function is doing everything it is suppose to. The problem, which was tackled by MMJ, is that my variable is private, and I am attempting to make the var "stuff" be global, defined by the check(x), and used in showme(stuff).

So on that note, MMJ, I did what you said, and it makes sense looking at it now (I have forgotten a lot of basics on my programming skills ). Now, when you click on the "show" button, you get an alert box, however it is blank, which makes sense because that is how it is defined. Before, I was getting nothing. So I have progress, just not what I want yet.

So with the var stuff = ''; we have stuff defined as ''. I click on first through third, and each time I get a popup from check(x), and the textarea shows either a or b, just as it is suppose to. However, then stuff loses its a or b definition, and is showing as '' when I click on show

** EDIT **

BTW, amanxman, I did your suggestion just to see what would happen, and changing "else if" to "else" ended up breaking the function, it did nothing when I click on a radio button Again, I am thankful for your brain input!
__________________
madd says check out the always free and ad-less Madd's World - Red Mage of the Net (madd74.com) or check out my forum or enjoy my free arcade and beat my awesome high scores if you can

Last edited by madd74; 05-Nov-2008 at 04:17 PM.. Reason: additional testing
MMJ's Avatar
MMJ MMJ is offline
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
05-Nov-2008, 06:38 PM #5
Quote:
So with the var stuff = ''; we have stuff defined as ''. I click on first through third, and each time I get a popup from check(x), and the textarea shows either a or b, just as it is suppose to. However, then stuff loses its a or b definition, and is showing as '' when I click on show
Not quite following you.

At what point does it "lose its definition"?
madd74's Avatar
Computer Specs
Member with 267 posts.
 
Join Date: Nov 2003
Location: Des Moines, IA
Experience: Advanced
05-Nov-2008, 08:11 PM #6
well, with the following

SCRIPT
var stuff
function check(x){
code
}
function showme(stuff){
code
}

Okay, so you click a radio button. Global VAR stuff now gets assigned a new value. When you click on the button, however, whatever VAR stuff was assigned is no longer there, it is null ''. The code changed to now reflect this:

Code:
<html>
<head>

<script type="text/javascript">
var stuff = '';
function check(x) {
  document.getElementById("yes").value=x;
  if (x=="a") {
    var stuff = "your mom";
  }
  else if (x=="b") {
    var stuff = "your dog";
  }
  alert(stuff);
  return stuff;
}
</script>

<script type="text/javascript">
  function showme(stuff)
  {
  alert(stuff);
  }
</script>

</head>
<body>

<form>
<input type="radio" name="browser" onclick="check(this.value)" value="a">first<br />
<input type="radio" name="browser" onclick="check(this.value)" value="a">second<br />
<input type="radio" name="browser" onclick="check(this.value)" value="b">third<br />
<br />
<input type="text" id="yes" size="2"><br />
<input type="button" onclick="showme(stuff)" value="show" />
</form>

</body>
</html>
Clicking on the "show" button brings up a blank alert box. It should also be noted that I attempted to define VAR stuff in its own text/javascript script, and I also attempted to move one function to the body and leave the other in the head and I still get the same results. The only part of this code that does not work, is that clicking show button is always blank.

Thanks again for everyone who has been looking into this for me.
__________________
madd says check out the always free and ad-less Madd's World - Red Mage of the Net (madd74.com) or check out my forum or enjoy my free arcade and beat my awesome high scores if you can
MMJ's Avatar
MMJ MMJ is offline
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
06-Nov-2008, 09:54 AM #7
Doesn't seem to be that way when I test it.

When I first load the page and click on show it is empty because stuff is only an empty string.

However when I click on one of the radios then click on show it shows either your mom or dog, as it should do.
madd74's Avatar
Computer Specs
Member with 267 posts.
 
Join Date: Nov 2003
Location: Des Moines, IA
Experience: Advanced
06-Nov-2008, 10:33 PM #8
hmm... that is absolutely crazy. I wonder if it has something to do with me testing with the tryit editor on w3schools. I will do this locally to see what I get, again, thanks a bunch!

** EDIT **

Hmm, nope, I get the same thing when I attempt it locally. I am using FF 3, so I am not sure if that has something to do with it. Just to verify, you get the alert box to show when you actually press the show button? Because it DOES give an alert box when you select a radio button, so I want to make sure we are on the same page
__________________
madd says check out the always free and ad-less Madd's World - Red Mage of the Net (madd74.com) or check out my forum or enjoy my free arcade and beat my awesome high scores if you can
MMJ's Avatar
MMJ MMJ is offline
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
07-Nov-2008, 07:26 AM #9
Yeah, when I click show an alert box pops up, which makes sense because that is what the code tells it to do.

Make sure you are using my code and try a hard refresh if you think the page may be cached.

I am also using Fx 3.
madd74's Avatar
Computer Specs
Member with 267 posts.
 
Join Date: Nov 2003
Location: Des Moines, IA
Experience: Advanced
07-Nov-2008, 06:05 PM #10
hmm... I am wondering if maybe it is, then. so the last code used is the one you checked and it worked with (post number 6)? if so, I will copy it, make an HTML file, and open it fresh on another computer that way I know it can't be a caching issue of any sort

** edit **

Okay, I actually see what I was doing different from your code, even though, I am not sure how that makes a difference since everything else worked fine. In my IF statements, I have "var stuff = " where you just have "stuff = " otherwise the code is identical. I even chopped your code, spacing wise, to be exactly like what I was using.

I always thought that "var thing = something" was the same as "thing = something"? Is it because it is being assigned in a function? If that is the case, do you know the reason it would actually work fine in the check(x) function? Does "var stuff = value" not store properly in the global variable of stuff then? That is what I am getting out of it :mrgreen:
__________________
madd says check out the always free and ad-less Madd's World - Red Mage of the Net (madd74.com) or check out my forum or enjoy my free arcade and beat my awesome high scores if you can

Last edited by madd74; 07-Nov-2008 at 06:26 PM.. Reason: MMJ is smarter than me :D
MMJ's Avatar
MMJ MMJ is offline
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
08-Nov-2008, 05:46 AM #11
var tells it to keep the variable private, so it isn't working on the global variable.

which means that the global variable will always be empty

btw, my code flattened like that because of some sort of forum bug, when i posted it it was fine.
Reply

Tags
function, javascript

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:28 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.