Is there a way to get the return-value of an onclick-function like confirm into a jQuery-function?
I have links which have an onclick-event for confirmation which returns true or false:
<a onclick="return confirm('Sure?');" class="delete" href="delete.php">Delete</a>
This is a given structure and I have no possibility in changing it.
But I would like to do something like this:
$('a.delete').click(function() {
if (confirm_from_onclick == true) {
//do some more before really returning true and following the link
}
});
If there is no way for you to change the HTML markup, you need to remove the complete
onclickinline-handler from that node and to it in your unobtrusive event handler.This could look like:
Since you’re dealing with an
onclickinline-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
onclicktonulland rewrite the logic in your own event handler. I did it for convenience only.Demo: http://jsfiddle.net/em9KP/