Baseline Widely available
The submit
event fires when a <form>
is submitted.
Note that the submit
event fires on the <form>
element itself, and not on any <button>
or <input type="submit">
inside it. However, the SubmitEvent
which is sent to indicate the form's submit action has been triggered includes a submitter
property, which is the button that was invoked to trigger the submit request.
The submit
event fires when:
form.requestSubmit()
methodHowever, the event is not sent to the form when a script calls the form.submit()
method directly.
Note: Trying to submit a form that does not pass validation triggers an invalid
event. In this case, the validation prevents form submission, and thus there is no submit
event.
Use the event name in methods like addEventListener()
, or set an event handler property.
addEventListener("submit", (event) => { })
onsubmit = (event) => { }
Event type Event properties
In addition to the properties listed below, this interface inherits the properties of its parent interface, Event
.
submitter
Read only
An HTMLElement
object which identifies the button or other element which was invoked to trigger the form being submitted.
This example uses EventTarget.addEventListener()
to listen for form submit, and logs the current Event.timeStamp
whenever that occurs, then prevents the default action of submitting the form.
<form id="form">
<label>Test field: <input type="text" /></label>
<br /><br />
<button type="submit">Submit form</button>
</form>
<p id="log"></p>
JavaScript
function logSubmit(event) {
log.textContent = `Form Submitted! Timestamp: ${event.timeStamp}`;
event.preventDefault();
}
const form = document.getElementById("form");
const log = document.getElementById("log");
form.addEventListener("submit", logSubmit);
Result Specifications Browser compatibility See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3