'on submit of dynamically generated form, find which submit button was clicked

I have multiple forms which are dynamically generated at runtime by a template engine, in order to generate an ajax request on their submission I have used the on method to delegate the event.

$("#friendRequestsList").on("submit", "[id^=friendReqResponse-form]", function(){
    var form = this;
    $(form).ajaxSubmit({
        url:'URL',
        type:'post',
        dataType: 'json',
        success: function(data) {
            DO SOMETHING
        }

    });
    return false;
});

The form has two submit buttons, and I need to detect which of them was clicked and pass this information to the ajax request. However I am having a hard time knowing which of the submit inputs in the form was clicked, as they have also been dynamically generated. Ideas?



Solution 1:[1]

I hope it will help you :

$("#friendRequestsList").on("submit", "[id^=friendReqResponse-form]", function(){
     var submit1 = $(this).attr('value');

     var formData = new FormData();
     formData.append('submit1', submit1);

     $.ajax({
        url:'URL',
        type:'post',
        data:formData,
        dataType: 'json',
        success: function(data) {
            DO SOMETHING
        }

    });
    return false;
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rohìt Jíndal