0

I have this code: http://jsfiddle.net/xFvrV/2/

<form id="formTest" action="#" method="post">
    <input type="text" id="textTest"/><br>
    <input id="submitButtonDraft" type="submit" value="Save Draft" /><br>
    <input id="submitButton" type="submit" value="Save" />
</form>

$(document).ready(function () {
    $("#formTest").submit(function (e) {
        e.preventDefault();
        alert('toto');
    });
});

How to know which button submit the form? and display it in the alert()

Thank you

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
FRO
  • 253
  • 1
  • 7
  • 17
  • possible duplicate of [Determine which element submitted a form from within onsubmit](http://stackoverflow.com/questions/541869/determine-which-element-submitted-a-form-from-within-onsubmit) – Lightness Races in Orbit Jun 20 '11 at 21:24
  • 1
    In terms of design it's bad form design if you _care_ about which button submitted the form. You really should have two forms instead if the submission button is important. – Raynos Jun 20 '11 at 21:30

2 Answers2

6

Keep track of the button pressed: http://jsfiddle.net/xFvrV/9/.

$(document).ready(function () {
    $("#formTest").submit(function (e) {
        e.preventDefault();
        // get data from form's data
        alert($(this).data('submitbutton').attr('value'));
    });

    $('input[type=submit]').click(function() {
        // save as data on form's .data
        $(this).parent().data('submitbutton', $(this));
    });
});
pimvdb
  • 151,816
  • 78
  • 307
  • 352
-4

If they are of type=submit they will submit the form.

So in your case both of the buttons will submit the form.

Naftali
  • 144,921
  • 39
  • 244
  • 303