Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining which submit button was clicked from jQuery/JavaScript [duplicate]

I'm ajaxifying some forms from a PHP application I didn't write. To do this, I came up with this clever solution:

jQuery("form").submit(function(event) {
    // get some values from elements on the page:
    var the_form = jQuery(this);
    var data = the_form.serialize();
    var url = the_form.attr( 'action' );
    var button = event.originalEvent.explicitOriginalTarget;

    data = data + "&" + button.name + "=" + button.value;

    // Send the data using post and put the results in a div
    jQuery.post( url, data, function() {
        //Do something crazy
    });

    // stop form from submitting normally
    if (event.preventDefault) 
    { 
        event.preventDefault(); 
    } 
    else 
    {
        event.returnValue = false; 
    }
});

Which works perfectly. I went away rejoicing. The problem is, I inadvertently used a Mozilla/Gecko only property to determine which button was clicked. (event.originalEvent.explicitOriginalTarget) Which means this only works in Firefox. :-(

All of this is necessary because the web app I'm augmenting relies on the button name/value being in the post data to process the form correctly. So, my question in simple terms would be:

What is the best, cross-browser way to determine which button was clicked in jQuery's submit event?

Edit: And here is my solution.

jQuery("some selector that targets your form").find(":submit").click(function(event) {
    // get some values from elements on the page:
    var the_form = jQuery(this).parents("form");
    var data = the_form.serialize();
    var url = the_form.attr( 'action' );
    var button = event.target;

    data = data + "&" + button.name + "=" + button.value;

    // Send the data using post and put the results in a div
    jQuery.post( url, data, function() {
        //Do something crazy
    });

    // stop form from submitting normally
    if (event.preventDefault) 
    { 
        event.preventDefault(); 
    } 
    else 
    {
        event.returnValue = false; 
    }
});
like image 837
clifgriffin Avatar asked Apr 20 '11 15:04

clifgriffin


People also ask

How do you know which submit button was clicked in jQuery?

activeElement will give you the submit button that was clicked. document. activeElement. getAttribute('value') will give you that button's value.

How do you check if submit button is clicked?

PHP isset() function is used to check if a variable has been set or not. This can be useful to check the submit button is clicked or not. The isset() function will return true or false value.

What happens when submit button is clicked?

5. Most HTML forms have a submit button at the bottom of the form. Once all of the fields in the form have been filled in, the user clicks on the submit button to record the form data. The standard behaviour is to gather all of the data that were entered into the form and send it to another program to be processed.


2 Answers

See this question: Crossbrowser equivalent of explicitOriginalTarget event parameter

You're going to have to attach the event listeners to the buttons instead of the form to get a good reliable way of determining which one fired the submit.

like image 188
greggreg Avatar answered Sep 17 '22 17:09

greggreg


http://api.jquery.com/event.target/

jquery.event.target should work because it is normalised for most browsers.

jquery.event.currentTarget can be used to retrieve the current item in the event bubbling chain.

Edit-- After some reflection and @greg's suggestion: I've posted a code snippet on jsfiddle.

like image 29
leon Avatar answered Sep 21 '22 17:09

leon