Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Determine Which Submit Button Was Pressed, Form onSubmit Event, Without jQuery [duplicate]

I have a form with two submit buttons and some code:

HTML:

<input type="submit" name="save" value="Save" /> <input type="submit" name="saveAndAdd" value="Save and add another" /> 

JavaScript:

form.onSubmit = function(evnt) {     // Do some asyncrhnous stuff, that will later on submit the form     return false; } 

Of course the two submit buttons accomplish different things. Is there a way to find out in onSubmit which button was pressed, so later I could submit by doing thatButton.click()?

Ideally I would like to not modify the code for the buttons, just have a pure JavaScript addon that has this behavior.

I know that Firefox has evnt.explicitOriginalTarget, but I can't find anything for other browsers.

like image 718
McTrafik Avatar asked Aug 26 '10 16:08

McTrafik


People also ask

Which event is triggered when a form submit button is pressed?

The submit event fires when the user clicks a submit button ( <button> or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form. submit() method directly.

Can a form have 2 submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

Can we use Onsubmit on button?

A Form can have onsubmit event but a button cannot have the onsubmit.


1 Answers

<form onsubmit="alert(this.submitted); return false;">     <input onclick="this.form.submitted=this.value;" type="submit" value="Yes" />     <input onclick="this.form.submitted=this.value;" type="submit" value="No" /> </form> 

jsfiddle for the same

like image 99
Ali Shakiba Avatar answered Oct 13 '22 00:10

Ali Shakiba