Log in

View Full Version : Resolved Return value from Javascript function



zomb1e
07-29-2012, 06:46 PM
Not sure what is the level of this question, but any help would be much appreciated.

I have 2 Javascript functions. One of them takes a value as an input. I want to return that same value from second function without passing it from first function to second function. I guess it can be done through Global variable, but not sure how it is done. I dont want to give any input for my second function, i.e. display().

Javascript:

<script language="JavaScript" type="text/javascript">

var p;
function a(imgsrc){

p = imgsrc;

}

function display(){
return p;
}

</script>

HTML:

<a onClick="a('imagesource');">Link</a>

I'm displaying variable return from display() function in HTML (as shown below)

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


Whenever Link is clicked, I want 'p' variable to be updated.

Thanks in advance!!!!!!!

bernie1227
07-29-2012, 09:47 PM
Hiya,
Two questions here,
One, Why can you not have the p variable in the second function?
Two, you haven't actually referenced either of the functions in the onclick

zomb1e
07-30-2012, 09:31 PM
Thanks four your response.

For your first question, I dont want to pass p variable in display() function coz in that case return value will be same as input. I want to use display() function only for returning 'p' value purpose which will be passed in a() function.
So basically I want to use value passed in a() function to be used as variable which will be returned from display() function.

For your second question, I dont understand about referencing. Is it necessary to reference your function. I guess I'm calling a() function through OnClick. And display() function is only being used to return a value which is fianlly displayed on the webpage.
Can you please correct me if I'm wrong. Thanks!!

keyboard
07-30-2012, 10:20 PM
Also -

Please use the forum's bbcode tags to make it more readable:

for php code............
<?php /* code goes here */ ?>
for html...............
<!-- markup goes here -->.....
for js/css/other.......
code goes here................

bernie1227
07-30-2012, 10:45 PM
Try:


<script language="JavaScript" type="text/javascript">
function a(imgsrc){

var p = imgsrc;

}

function display(){
document.write(p);
}

</script>




<a onClick="display();">Link</a>

zomb1e
07-30-2012, 11:16 PM
Hey I like this approach. Also it will solve my problem as we are not passing anything through display() function and desired value gets printed out by directly calling display().

BUT

This code is not working for me. Was it working for you?
I tried it in a different way though. I called the function display() directly rather than calling it through onClick. But its not working.


<script type="text/javascript">
display();
</script>

keyboard
07-31-2012, 12:11 AM
Bernie's code won't work because your trying to call function display before you've called function a (the p variable is never set).

Also, you said that when you click on the link, you want the p variable to be updated... his above script doesn't do that...

This script should do what your asking..... maybe :D


<!DOCTYPE html>
<html>
<head>
<script language="javaScript" type="text/javascript">
function a(imgsrc){
p = imgsrc;
display();
}
function display(){
document.write(p);
}
</script>
</head>
<body>
<a onclick="a();">Link</a>
</body>
</html>

bernie1227
07-31-2012, 01:09 AM
Ah, that looks about right, so your calling the actuall display function when you're initializing the variable.

zomb1e
07-31-2012, 11:12 PM
Thanks a lot everyone!!!!

Its working now.

U guys ROCK!!!!!!

keyboard
08-01-2012, 12:49 AM
One more thing -

If this thread is finished, please set it to resolved.
You can do this by editing the first post within the thread - Pressing go advanced - Then where it says no prefix, selecting resolved then save.