HTMLFormElement: submit event

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("HTML DOM")}}

The **submit** event fires when a {{HtmlElement("form")}} is submitted.

Syntax

Use the event name in {{domxref("EventTarget/addEventListener", "addEventListener()")}}:

js-nolint
addEventListener("submit", (event) => { })

Alternatively, assign to the event handler property of the interface:

js-nolint
onsubmit = (event) => { }

Event type

A {{domxref("SubmitEvent")}}. Inherits from {{domxref("Event")}}.

{{InheritanceDiagram("SubmitEvent")}}

Description

The submit event fires on the <form> element itself, and not on any {{HtmlElement("button")}} or {{HtmlElement('input/submit', '&lt;input type="submit"&gt;')}} inside it. However, the {{domxref("SubmitEvent")}} which is sent to indicate the form's submit action has been triggered includes a {{domxref("SubmitEvent.submitter", "submitter")}} property, which is the button that was invoked to trigger the submit request.

The submit event fires when:

  • the user clicks a {{Glossary("submit button")}},
  • the user submits the form implicitly,
  • a script calls the {{domxref("HTMLFormElement.requestSubmit()", "form.requestSubmit()")}} method

However, the event is _not_ sent to the form when a script calls the {{domxref("HTMLFormElement.submit()", "form.submit()")}} method directly.

Trying to submit a form that does not pass validation triggers an {{domxref("HTMLInputElement/invalid_event", "invalid")}} event. In this case, the validation prevents form submission, and thus there is no submit event.

Implicit submission

The HTML specification does not formally define what user gestures may trigger implicit submission, because it depends on operating system and device conventions. For example, pressing <kbd>Enter</kbd> while focusing on a text input is a common gesture. However, the browser's reaction to such a gesture is standardized:

  • If the form has a non-disabled default submit button, the browser fires a click event at that button. The default button is the first submit button in tree order whose form owner is that form. This then triggers the form submission process (as if the button has been pressed by the user), unless the event is canceled.
  • If the form has no submit button, it is submitted implicitly only when it has at most one {{HTMLElement("input")}} element of type text, search, tel, url, email, password, date, month, week, time, datetime-local, or number. {{HTMLElement("textarea")}} and {{HTMLElement("select")}} elements do not block implicit submission.
  • Otherwise, if the form has a disabled default submit button, or no submit button and more than one input blocking implicit submission, the gesture never triggers implicit submission.

Examples

This example uses {{domxref("EventTarget.addEventListener()")}} to listen for form submit, and logs the current {{domxref("Event.timeStamp")}} whenever that occurs, then prevents the default action of submitting the form.

HTML

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

js
const form = document.getElementById("form");
const log = document.getElementById("log");

function logSubmit(event) {
  log.textContent = `Form Submitted! Timestamp: ${event.timeStamp}`;
  event.preventDefault();
}

form.addEventListener("submit", logSubmit);

Result

{{EmbedLiveSample("Examples", "", "", "", "", "", "", "allow-forms")}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • HTML {{HtmlElement("form")}} element
  • Related event: {{domxref("HTMLInputElement/invalid_event", "invalid")}}