3

I have created an anchor with an onclick event which calls a JavaScript function. The JavaScript function returns some value. I want to use that value in another JS function.

e.g loading() will return some value which will get passed to another js function. How do I capture and store the return value, and then pass this value to that function?

gariepy
  • 3,576
  • 6
  • 21
  • 34
Ganesh
  • 505
  • 2
  • 9
  • 26
  • 1
    As evidenced by your comments below, you've done a poor job in this question of explaining exactly what you need. Perhaps you should edit your question to clarify exactly what the user experience (user actions and system responses) should be. – Phrogz Apr 07 '12 at 13:29
  • I agree... but since your new, no downvotes. – Chris Gessler Apr 07 '12 at 13:37

4 Answers4

5

Can you simply call the outer function with the inner function?

function outerFunc(a)
{
  alert(a);
}

function innerFunc()
{
  return 'test';
}

onclick="outerFunc(innerFunc());"

Or, if you need to use the return value in another event, set a variable.

var retval;
function outerFunc()
{
  if(retval) alert(retval);
}

function innerFunc()
{
  retval = 'test';
  return retval;
}

onclick="return innerFunc();"

In someother onclick event

onclick="return outerFunc();"
Phrogz
  • 296,393
  • 112
  • 651
  • 745
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
  • situation is like that onclick returns value which i have to store and pass for other js function which will get called while clicking on another anchor. – Ganesh Apr 07 '12 at 12:52
  • -1 for using the `onclick` attribute instead of attaching the event handler in JavaScript. – Phrogz Apr 07 '12 at 13:17
  • sorry but its not working,i guess Global variables are deleted when you close the page – Ganesh Apr 07 '12 at 13:18
  • @Phrogz - so you're saying that using a built-in attribute is a bad thing? I think you're nuts. – Chris Gessler Apr 07 '12 at 13:19
  • @Jack - yes, global variables die when the page is closed. Your only alternatives would be to use a cookie or pass the value back to the server for storage (session, database, etc.) and recall it when the user revitis the page. – Chris Gessler Apr 07 '12 at 13:21
  • @Chris …or pass it in the query string link to the new page. (+1 for your persistence in helping. :) – Phrogz Apr 07 '12 at 13:30
  • @Phrogz - yes, the query string would one possible way of passing the variable back to the server. Form post, AJAX (xml, json). I think the details of the question are lacking so I don't have much of clue on how to help. – Chris Gessler Apr 07 '12 at 13:34
  • @ChrisGessler thanks for your awesome answer but "onclick="return innerFunc();" In someother onclick event onclick="return outerFunc();"" is very confusing to me. can you explain more please? :) **you perfectly demonstrated how functions are first class in JavaScript I guess?** – Kick Buttowski Oct 08 '15 at 21:25
  • @KickButtowski - The first onclick sets a variable, the second onclick consumes that variable. If the second onclick fires first, the variable isn't set, so nothing happens. – Chris Gessler Oct 09 '15 at 12:00
0

a) Use a global variable e.g (also)

var passVar=null;

function A(){
  //set value
  passVar='hello';
}

function B(){
  //get value 
  alert(passVar);
}

b) if your function is on ANOTHER page which is consuming the stored value, you might be using setItem() ,getItem() and more advance features of browsers to use browser session storage mechanism

Community
  • 1
  • 1
sakhunzai
  • 13,900
  • 23
  • 98
  • 159
  • @Phrogz- it is working with cookies, since i want to use return value on another page, i have stored that into cookie and used it again,all of you thanks for help. – Ganesh Apr 09 '12 at 06:44
0

You mentioned in a comment you want to save it for later. You can use the fact that JavaScript functions are closures and thus have access to local variables declared in the same scope:

var clickedValue; // No need to set a value here, just declare is

myAnchor.addEventListener('click',function(evt){
  // Call the other function, setting the 'this' scope to be the anchor
  // The scope of the variable set it the one above
  clickedValue = someFunction.call(myAnchor,evt);
},false);

/* later on… */

otherFunction(clickedValue);
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • I've edited my answer to account for the fact that you mentioned in a comment that you don't want to use the returned value immediately. – Phrogz Apr 07 '12 at 13:27
-1

Something like this?

<a onClick="loading(myFunction());">

If myFunction() is your original onClick code, it will get passed to loading afterward.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592