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.
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.
yes, multiple submit buttons can include in the html form. One simple example is given below.
A Form can have onsubmit event but a button cannot have the onsubmit.
<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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With