Open which button of the button starts sending
In my form, I have two submit buttons.
<input type="submit" name="save" value="Save as draft"/> <input type="submit" name="send" value="Start sending" /> Now, before submitting the form back to the server, I want to perform certain tasks depending on which button was clicked.
$('form').submit(function(){ if submit equals save task 1; elseif submit equals send task 2 } How to check which button was pressed?
If it were me, I would have done it a little differently. I would do something like:
<button type='button' id='formSave'>Save as draft</button> <button type='button' id='formSend'>Start Sending</button> $('#formSave').click(function(){ //submit form //task 1 }); $('#formSend').click(function(){ //submit form //task 2 }); I'm just basing my answer on your button names and something else, but it looks like you want to do something similar to this.
$('input').each(function(n) { $(this).click( function() { var name = $(this).attr('name'); if (name === 'save') { // do stuff }else if (name === 'submit'){ // do some other stuff } } }); Of course, you can also use the switch statement.
If the task you want to complete is complex and different, you should consider using two different forms.
Hope this answers your question.
EDIT:
$('input:submit').click(function(n) { var name = $(this).attr('name'); if (name === 'save') { // do stuff }else if (name === 'submit'){ // do some other stuff } } }); I suggest using the HurnsMobile approach, but if you absolutely need one function to handle both buttons, you can use the jQuery event object to detect the buttons,
$('form').submit(function(event){ if(event.target.name == 'save') { // do save task } else if (event.target.name == 'send') { // do send task } });