Any way to pass parameters programmatically to an onclick function?

Posted by RenderIn on Stack Overflow See other posts from Stack Overflow or by RenderIn
Published on 2010-04-26T19:50:02Z Indexed on 2010/04/26 19:53 UTC
Read the original article Hit count: 467

I have an onclick function which performs several tasks. In another javascript function I do not have access to the context variables needed to perform these tasks. To get around this I have been simply calling the onclick function directly.

The problem I have now is that I'd like to perform a task after an Ajax action in the onclick completes. Is there any way for me to pass a function to the onclick method of a link? What would the onclick attribute look like?

e.g. something like this:

<a id="link3" href="javascript:void(0);" onclick="function(callback) { X(a); Y(b); Z(c, callback); };">click me</a>

Clicking on this would pass "undefined" as the callback, while I could also call it explicitly like this:

document.getElementById("link3").onclick(function() { alert("Completed all tasks"); } );

Is something like this possible? Basically I want to be able to pass an optional parameter to the onclick method, but if it's absent I want it to behave as if there were just procedural code in the onclick.

© Stack Overflow or respective owner

Related posts about javascript-events

Related posts about JavaScript