JQuery: Get return of an onclick-function from an anchor?

If there is no way for you to change the HTML markup you need to remove the complete onclick inline-handler from that node and to it in your unobtrusive event handler This could look like: $(function() { $('a'). Each(function(_, anchor) { var onclk = anchor. Onclick; // store the original function anchor.

Onclick = null; // delete/overwrite the handler $(anchor). Bind('click', function() { if( onclk() ) { // do something } return false; // call stopPropagation() + preventDefault() }); }); }) Since you're dealing with an onclick inline-event handler, you cannot stop or prevent that from firing with an unobtrusively bound event handler. It'll always fire first, so you need to completely remove the original event You don't really need to store the function, you also could just set the onclick to null and rewrite the logic in your own event handler.

I did it for convenience only Demo : jsfiddle.net/em9KP.

If there is no way for you to change the HTML markup, you need to remove the complete onclick inline-handler from that node and to it in your unobtrusive event handler. This could look like: $(function() { $('a'). Each(function(_, anchor) { var onclk = anchor.

Onclick; // store the original function anchor. Onclick = null; // delete/overwrite the handler $(anchor). Bind('click', function() { if( onclk() ) { // do something } return false; // call stopPropagation() + preventDefault() }); }); }); Since you're dealing with an onclick inline-event handler, you cannot stop or prevent that from firing with an unobtrusively bound event handler.It'll always fire first, so you need to completely remove the original event.

You don't really need to store the function, you also could just set the onclick to null and rewrite the logic in your own event handler. I did it for convenience only. Demo: jsfiddle.net/em9KP.

JAndy - This is a nice solution. Could you please just explain this one bit: $('a'). Each(function(underscore, anchor), specifically the function(underscore, anchor) part?

I've never seen this before. (Comments don't like underscores apparently) – JackWilson Mar 29 at 8:00 @JackWilson: The .each() method passes two things into it's callback. The index and the actual element.

Since I have no application for the index value in this snippet, I just use an underscore. I could also just write "index" or "foobar". – jAndy Mar 29 at 8:04 Very good solution, thank you very much!

– Tim Mar 29 at 8:06.

As in, create the confirm dialog inside the function. Delete $('a. Delete').

Click(function(e) { e.preventDefault(); if (confirm('Sure? ')) { //do some more before really returning true and following the link } }); EDIT: Sorry! Missed the part where you mentioned that it's a set structure and you have no way of changing it.

Working on an alternative.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions