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?

2
  • 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. Commented Apr 7, 2012 at 13:29
  • I agree... but since your new, no downvotes. Commented Apr 7, 2012 at 13:37

4 Answers 4

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();"
Sign up to request clarification or add additional context in comments.

9 Comments

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.
-1 for using the onclick attribute instead of attaching the event handler in JavaScript.
sorry but its not working,i guess Global variables are deleted when you close the page
@Phrogz - so you're saying that using a built-in attribute is a bad thing? I think you're nuts.
@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.
|
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

1 Comment

@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.
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);

1 Comment

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.
-1

Something like this?

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

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

1 Comment

actually i want to store that return value and use it afterwards not in same call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.