admin管理员组文章数量:1279182
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?
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?
- 1 As evidenced by your ments 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 Commented Apr 7, 2012 at 13:29
- I agree... but since your new, no downvotes. – Chris Gessler Commented Apr 7, 2012 at 13:37
4 Answers
Reset to default 5Can 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();"
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
You mentioned in a ment 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);
Something like this?
<a onClick="loading(myFunction());">
If myFunction()
is your original onClick code, it will get passed to loading
afterward.
本文标签: htmlreturn onclick value to a JavaScript functionStack Overflow
版权声明:本文标题:html - return onclick value to a JavaScript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741234061a2362651.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论