HTMLFormElement: submit event
The submit
event fires when a <form>
is submitted.
Bubbles | Yes (although specified as a simple event that doesn't bubble) |
---|---|
Cancelable | Yes |
Interface | SubmitEvent |
Event handler property | GlobalEventHandlers.onsubmit |
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 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.
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.
Examples
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.
HTML
<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! Time stamp: ${event.timeStamp}`; event.preventDefault(); } const form = document.getElementById('form'); const log = document.getElementById('log'); form.addEventListener('submit', logSubmit);
Result
Specifications
Specification |
---|
HTML Standard (HTML) # event-submit |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
submit_event |
1 |
12 |
1 |
9 |
8 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event