1

I have the following jQuery:

$('form').one('submit', function(e) {
    e.preventDefault();
    var id = $(this).data("id");
    $("input.id-"+id+".column-name").val($("span.column-name.id-"+id).html());
    $("input.id-"+id+".column-ext").val($("span.column-ext.id-"+id).html());
    $("input.id-"+id+".column-dept").val($("span.column-dept.id-"+id).html());
    $(this).submit();
}); 

It works great, except for some reason now it leaves out the name of the button that was clicked. Each form on the page has two buttons, 'save' and 'delete'. Clicking either one of these submits the form, and posts the data but not the data of the submit button (they both have corresponding names).

If I remove the above code, and just submit the form as usual, I get the button that was pressed.

Chud37
  • 4,907
  • 13
  • 64
  • 116
  • It's because when you call the `submit()` function through code you lose the associated click event on the button, hence no button data can be added to the request – Rory McCrossan Aug 23 '17 at 08:50
  • is there a way to detect which button was pressed? – Chud37 Aug 23 '17 at 08:51
  • Not from a programmatically triggered event, no. Usually you could trigger a click event on the button, however that won't work for you as you'll end up in an infinite loop – Rory McCrossan Aug 23 '17 at 08:52
  • 1
    You could f.e. pass the necessary data via a hidden input field ... you’d need to find out which button was clicked (so you might want t handle click events on the buttons in the first place, instead of the form’s submit event), then you add/populate the hidden field with the appropriate value, and then let the form submit. – CBroe Aug 23 '17 at 08:55

1 Answers1

0

You can print the value of the button that is active by using the following code:

$("form").submit(function() {
console.log($(document.activeElement).val());
}

I would suggest you to refer the following link where the answers for all kind of errors related to submit button-JQuery are discussed

jQuery: how to get which button was clicked upon form submission?

DivyaMaheswaran
  • 886
  • 1
  • 12
  • 16