This StackOverflow question asks to find the button, that triggered a submit
form event from within an submit
event handler. Use cases for this are a dime a dozen, when you think of serializing the form data for AJAX requests and more than one submit buttons in the <form>
.
My answer was to use document.activeElement
for this, but it has some shortcomings, like any of the other answers, too.
Therefore adding a property to the dispatched submit
event that links to the initiator of the submission would help here. The property references the DOM element, that was activated for the form submission. Something along the following lines:
Property
submitter
on an event of typesubmit
When a form is submitted, the
submit
event created according to HTML5.1, Form submission algorithm contains a propertysubmitter
.
- If the form was submitted via click on a submit button,
event.submitter
references the button.- If the form was submitted implicitly, e.g., by hitting
enter
in a text input field,event.submitter
references the form’s default button as defined in Implicit submission- If the form was submitted with its
submit()
method, setevent.submitter = null
.