16

This should be really straight forward.

I'm checking if a form is being submitted using jquery. The form has multiple submit buttons with various values:

<button type="submit" value="foo">Foo</button>
<button type="submit" value="bar">Bar</button>

I would like to find the value of the button that just submitted the form:

$(form).live('submit', function() {
    // Get value of submit button
}

Thanks

6 Answers6

25

Bind the event to submit buttons instead of the form and then $(this) would have the reference to the buttons...

$(":submit").live('click', function() {
    alert($(this).val());
})

EDIT As of jQuery 1.7 live is deprecated. Use on instead.

$(document).on("click", ":submit", function(e){
    alert($(this).val());
});
amit_g
  • 30,880
  • 8
  • 61
  • 118
  • Per docs, [.live()](http://api.jquery.com/live/) is deprecated in favor of [.on()](http://api.jquery.com/on/) – CrazyPyro Jun 14 '13 at 16:56
  • @CrazyPyro, This question was asked in Mar 2011 and jQuery 1.7 that deprecated live was released on Nov 2011. – amit_g Jun 14 '13 at 18:34
6

This seems to get the clicked submit button:

jQuery("#TheForm").context.activeElement.name
jQuery("#TheForm").context.activeElement.value
Jesse
  • 8,605
  • 7
  • 47
  • 57
Mattias
  • 87
  • 1
  • 1
3

You can't if you're using the "submit" event. A form can be submitted without ever hitting a submit button.

In order to obtain which button was clicked, you need to alter your approach by using custom events and marking down which button was actually clicked (creating a selector that can catch click on any of the X buttons in the form). That also requires binding a custom event and prohibiting submit event of the form itself.

Furicane
  • 1,173
  • 6
  • 18
1
$('input[type="submit"]').click(function() {
    alert ($(this).val());
}
Grim...
  • 16,518
  • 7
  • 45
  • 61
0

I recommend the use of the Firebug Add On, you get a lot of responses to your questions just looking to the data in the console. I realize that in your case you can access the submit value this way:

alert($(this).context.submit.value);
  • 2
    Maybe I'm using it in the wrong place - but I got "undefined" when I tried this. I had it inside here $('form').submit(function () { ... } – Jen Jan 16 '13 at 00:26
0

I ended up using the click event on each submit button, as others have suggested. That works fine, and you can return false from them if you want to cancel the submit. Just don't try to also bind the submit event of the form. You could also have each of these handlers call a common submit-handler function, passing their own value as an argument.

CrazyPyro
  • 3,257
  • 3
  • 30
  • 39
  • The following is an interesting approach, but ONLY works on Firefox: (see also http://stackoverflow.com/questions/179826/crossbrowser-equivalent-of-explicitoriginaltarget-event-parameter ) `$('form').submit( function(event) { var whichClicked = event.originalEvent.explicitOriginalTarget.id; //if ( whichClicked == "approve" ) // etc... });` – CrazyPyro Jun 08 '11 at 15:13