Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the button that was clicked from the form submit event in jquery?

I have a function that I'm using to prevent multiple postbacks of a form:

var submitted = false;
$(function() {
    $('form').bind('submit', function(e) {
        if (!submitted && CanSubmit(e)) {
            submitted = true;
            return true;
        } else {
            return false;
        }
    });
});

In the CanSubmit method, I need to interrogate the button that was clicked to determine whether I should allow the submit or not.

Note that I can't bind to specific click events - see this previous question for more details.

In Firefox, I can use e.originalEvent.explicitOriginalTarget, but this is apparently not available in IE.

How can I get this value from the e parameter in a cross-browser way?

like image 858
Damovisa Avatar asked Sep 07 '10 23:09

Damovisa


1 Answers

Actually you could just replace $('form').bind('submit' with $(':submit').bind('click' and your code would work just as well (and you could use this to see what was clicked).

like image 132
Tgr Avatar answered Sep 24 '22 17:09

Tgr