Use the event name in methods like addEventListener()
, or set an event handler property.
addEventListener("submit", (event) => {});
onsubmit = (event) => {};
Event type
A Event
.
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.
Examples
This example uses 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! Timestamp: ${event.timeStamp}`;
event.preventDefault();
}
const form = document.getElementById("form");
const log = document.getElementById("log");
form.addEventListener("submit", logSubmit);
Result
Specifications
Specification |
---|
HTML # handler-onsubmit |
Browser compatibility
BCD tables only load in the browser