views:

328

answers:

1

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.

EDIT: Added more specifics

The use case is that the onclick calls a search function which requires session variables. When regular searches are performed there is no problem, but when I wish to refresh the search results (e.g. when a the user edits or deletes one of the returned results) I need to call the same search function that was called previously and then print out a feedback message after they are refreshed. This feedback message output is what I would like to be in the callback method. I'd also like to set the window.location.href to page down to the affected row in the event of an edit or an insert.

I have access to several variables on the server side which I print out directly to the onclick attribute when I render the page. In other Javascript files that I am including on my page I would also like to be able to execute the same functions contained within the onclick. The only problem being that I would have to pass all these variables to those functions indirectly through several intermediate methods in the call chain.

The other option is to print these values to hidden page values and then retrieve them from within my method, but I would like to avoid this as it would be cluttering up everything just so that I could decorate my function call with some visual after-effects.

+1  A: 

Although this may be possible somehow, I would recommend not to add arguments to the native DOM events.

I can't see a real architectural need for this, either. Can you make a full example of a use case where you need this? I'm pretty sure someone can come up with an alternative solution that doesn't make additional arguments necessary.

Wouldn't, in your example, the right place to place the alert be the success callback of the Ajax request?

Pekka
I suppose so... I tried to have a single javascript AJAX call but that doesn't work very well when I wish to have callback behavior differ based on where it was called. I guess I can't use the shortcut of delegating to a switch-type method call and will need to call functions directly in this case. Sorry if this sounds vague... it's rather difficult to explain without copying all my code in and I can't do that in this particular case.
RenderIn
@RenderIn mmm, I think your needs are being served already. You can get hold of the element that triggered the `onclick` event - it differs slightly between browsers, there's bubbling and other issues, and I prefer to use a framework like jQuery for that, but it's possible, you will always get a full event object. You can then make your Ajax request and pass any important information (like, the element clicked) to the callback function. I think this should work out fine.
Pekka