1

If I have 2 submit buttons on a form:

<input name="submit" type="submit" value="Button 1">
<input name="submit" type="submit" value="Button 2">

I have this jquery but every time I click button 2 I get the alert for button 1.

$('form.profile').submit(function() {
if ($("input[type=submit]").val() == 'Button 1')
{
alert('you clicked button 1');
return false;
}
elseif ($("input[type=submit]").val() == 'Button 2')
{
alert('you clicked button 2');
return false;
}
});

I need to perform form validation before the form is sent to the server and I need to be able to determine which of the 2 buttons was clicked.

Is it possible to identify which of the 2 buttons caused the form to be submitted using jquery?

H. Ferrence
  • 7,906
  • 31
  • 98
  • 161

1 Answers1

0

Does this help!!! http://jsfiddle.net/hun4U/

Instead of formSubmission you can use click events.

HTML:-

<input name="submit" id="submit1" type="submit" value="Button 1">
<input name="submit" id="submit2" type="submit" value="Button 2">

Script

$('#submit1').click(function(){
//do your actions here
});

$('#submit2').click(function(){
    //do your actions here
    });
PSL
  • 123,204
  • 21
  • 253
  • 243
  • It really doesn't help @PSCoder. I already knew about the .click() What I need to do is form validation and I need to do a different validation based on which of the 2 buttons were clicked prior to sending the request back to the server. So I need to work within the .submit() and not do a direct .click() call. – H. Ferrence Mar 21 '13 at 10:38
  • Surprisingly you accepted this as answer after a long time. Thank you!!. But i thought initially you had it downvoted... :) – PSL Dec 01 '14 at 03:57