Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

`submit` and `input` events on HTML forms

Use `input` to react to edits and `submit` to handle form submission at the right point in the validation flow.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

form.addEventListener("submit", onSubmit);
form.addEventListener("input", onInput);
form.requestSubmit();
form.requestSubmit(submitter);
form.submit();

HTMLFormElement submit/input events serve different purposes. input starts on the edited control and bubbles up the DOM tree. In a listener on the form, event.target identifies the control and event.currentTarget identifies the form while the listener runs. A control associated by the form attribute but outside the form does not bubble to it just because they are associated. Use input for live feedback; change marks a committed edit, usually when a text field loses focus. Assigning to .value does not fire input by itself.

submit starts on the form, not on the button. It is a bubbling, cancelable SubmitEvent. Listen for it instead of a button's click so you also handle implicit submission, such as Enter where supported. event.submitter identifies the submit button, or is null when no button initiated submission. To stop the default submission, call event.preventDefault() in the listener before any await.

form.requestSubmit() follows the normal submission rules without selecting a button. form.requestSubmit(button) uses that button and respects overrides such as formaction and formnovalidate. If constraint validation is not skipped and fails, invalid controls receive invalid and no submit is fired. Passing an element that isn't a submit button throws TypeError; passing a submit button not owned by that form throws NotFoundError.

Calling form.submit() directly skips constraint validation and does not fire submit. A control with name or id equal to submit can mask the method. Browser validation is not a substitute for server-side validation. These are web-platform APIs, not ECMAScript 2024 features; check support in the runtime you target.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Form submit and input events</title>
</head>
<body>
<script>
(() => {
  const form = document.createElement("form");
  const field = document.createElement("input");
  field.type = "text";
  field.required = true;

  const button = document.createElement("button");
  button.type = "submit";
  button.value = "save";
  button.textContent = "Save";
  form.append(field, button);

  field.addEventListener("invalid", (event) => {
    event.preventDefault();
    console.log("invalid");
  });
  form.addEventListener("input", (event) => {
    if (event.target === field) console.log(`input: ${field.value}`);
  });
  form.addEventListener("submit", (event) => {
    event.preventDefault();
    console.log(`submit: ${event.submitter.value}`);
  });

  document.body.append(form);
  try {
    form.requestSubmit(button);
    field.value = "Ada";
    console.log(`set: ${field.value}`);
    field.dispatchEvent(new Event("input", { bubbles: true }));
    form.requestSubmit(button);
  } finally {
    form.remove();
  }
})();
</script>
</body>
</html>

Result

invalid
set: Ada
input: Ada
submit: save

Save the example as an HTML file and open it in a browser. Open the console and reload the page. The first request logs `invalid`. After `set: Ada`, only the manually dispatched event produces `input: Ada`; the second request logs `submit: save`, but the listener cancels submission.

Related lessons

  • JavaScript · Lesson 15 · Advanced · Version: 4

Sources