tags:

views:

453

answers:

5

I have a number of links, that when clicked on, passes a variable thru to another portion of the page.

Yet, for some reason, I can’t figure it out! What am I missing?

<head>
<script type="text/javascript">

function myFunction(a){
  myid="Hi There!"+a;
  return myid;
}
</script>
</head>
<body>
<a href="#" OnClick="myFunction(123456);">Click Me</a><br />


<script type="text/javascript">
document.write(myid);
</script>

</body>
+3  A: 

You are getting a little mixed up here. Even though the function returns a value, it has nothing to return it to. Try this:

    <head>
    <script type="text/javascript">

    function myFunction(a){
      myid="Hi There!"+a;
      document.getElementById("debug").innerHTML = myid;
    }
    </script>
    </head>
    <body>
    <a href="#" OnClick="myFunction(123456);">Click Me</a><br />


<div id="debug"></div>

</body>
Tom Gullen
how about writing `return false;` in `myFunction`
Rakesh Juyal
return false isn't necessary if you don't need to return a value in this instance, just take the return value out completely.
Tom Gullen
@Rakesh – Moreover, you'd have to use `return myFunction(…)` in the `onclick` attribute, otherwise returning `false` doesn't prevent the default behaviour.
Marcel Korpel
@Tom,@Marcel: Damn! i missed that.
Rakesh Juyal
Thank you so much! I'm new to Stack Overflow. This is great and helped me understand the problem/solution better. Thanks!
rumblestrut
A: 

if you want to use it later you need to declare myid as a global variable. its scope is currently only within myFunction. also the document.write() function will only execute at runtime so you need to have another function the executes that with every click, or just combine the two.

JiminyCricket
Did you test that? I don't think that works...
Nick
I believe if the variable does not have a VAR in front of it, it is global in scope, not that that makes any difference here as it is the return value that is desired.
Mark Schultheiss
Oops! what are you trying to do?
Rakesh Juyal
typed before i tested it. oops. edited now.
JiminyCricket
A: 

When you click the link all that happens is that the myFunction() is called which returns the string. The line document.write(myid); is not executed anymore so nothing is visible.

Nick
A: 

Here is a simple example of some similar stuff:

<a href="#" type="button" OnClick="myf=myfunc(1234);alert(myf)" value="clickme">clickme</a> or  <a href="#" type="button" OnClick="myf=myfunc(1234);document.getElementById('other').value=myf;" value="clickme">ClickMeAlso</a>
<input id='other' type='text'/>

<script>
function myfunc(a) {
    return a + " howdy";
};
</script>

You can see this in action here:http://jsfiddle.net/5Sbn2/

Mark Schultheiss
Calling document.write after the page is complete will wipe out the page.
David Dorward
WELL YES, but it does demonstate the technique, the OP can decide what is need to be done with the value. Notice the same in the OP example code?
Mark Schultheiss
Edit to put in alert(_) instead even though OP had that docmment.write in there - and I do NOT agree that mimic of that was worthy of downvotes.
Mark Schultheiss
A: 
<script>
// This is global
var myid = ''

myfunc = function(a){
    myid = "Hi There!" + a;
    alert(myid);
}

test_global = function(){
    alert(myid);
}
</script>

<a href="javascript:void(0);" onclick="myfunc(12345);">Set MYID</a>
<input type="button" onclick="test_global();" value="Test MYID" />