Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which submit button was clicked

How can I know that which submit button was clicked on submit the form.

HTML:

<button type="submit" name="add_in_queue" id="add_in_queue" value="add_in_queue">Queue</button>
<button type="submit" name="create_tran" id="create_tran" value="create_tran">Process</button>

JQUERY:

$('form#create_tran_form').on('submit',function(e){
    var submit_value = $(this).attr('id');
    alert(submit_value);
    e.preventDefault();
});

I am getting the form ID that is: create_tran_form. I want to get submit button's name or value. How can I achieve this? Thanks.

like image 561
Ronak Patel Avatar asked Feb 01 '26 15:02

Ronak Patel


1 Answers

There is no way to tell from the submit event. You need to capture the event on the button.

e.g. have an event handler that listens for submit button clicks, store the result on the form, then read it back in on your submit handler.

$('[type="submit"]').on('click', function (evt) {
    $(this.form).data('selected', this.value);
});

$('form').on('submit', function (evt) {
    alert($(this).data('selected'));
});
like image 159
Quentin Avatar answered Feb 03 '26 05:02

Quentin