requestSubmit() vs submit(): validation and events

Use requestSubmit() when a script should submit a form with normal validation and submit events. The older submit() method bypasses both. Direct submission looks like this:

form.submit();

To follow normal validation and event handling, use:

form.requestSubmit();

A normal user submission

Imagine:

<form id="contact">
  <input
    type="email"
    name="email"
    required>

  <button type="submit">
    Send
  </button>
</form>

When the user presses Send, the browser:

  1. checks form validation;
  2. fires the submit event;
  3. uses the clicked submit button;
  4. submits if nothing prevents it.

That is normal form behaviour.

form.submit()

Now run:

form.submit();

This submits the form directly. Historically, that method bypasses normal interactive submission behaviour, including constraint validation and the normal submit event path. You may have:

form.addEventListener(
  "submit",
  event => {
    console.log("submitted");
  }
);

and expect the handler to run. Calling:

form.submit();

does not behave like clicking the button.

requestSubmit()

Instead:

form.requestSubmit();

asks the form to perform a normal-style submission. That means browser validation can run and the submit event is fired. Conceptually:

requestSubmit()
↓
behave like submit button

versus:

submit()
↓
submit directly

Validation example

<input
  type="email"
  required>

If the field is empty:

form.requestSubmit();

lets the browser stop the submission and show validation feedback. Calling:

form.submit();

can bypass that constraint-validation step.

Pass a submit button

You can also specify which submit button should act as the submitter:

form.requestSubmit(
  saveButton
);

That matters when a form has several submit buttons. For example:

<button
  type="submit"
  name="action"
  value="draft">
  Save draft
</button>

<button
  type="submit"
  name="action"
  value="publish">
  Publish
</button>

The chosen button can affect submitted data and attributes.

The submitter value

Inside the submit event:

form.addEventListener(
  "submit",
  event => {
    console.log(
      event.submitter
    );
  }
);

event.submitter tells you which button triggered the submission.

That is much cleaner than attaching separate submit logic to every button.

A custom shortcut

Imagine Ctrl+Enter should submit a form:

textarea.addEventListener(
  "keydown",
  event => {
    if (
      event.ctrlKey &&
      event.key === "Enter"
    ) {
      form.requestSubmit();
    }
  }
);

Now the keyboard shortcut follows normal validation and submit handling. That is exactly what you usually want.

When would submit() still be useful?

There are cases where direct submission is intentional. Maybe you have already performed all checks and specifically want to bypass normal event handling. But that is the unusual case.

For most application code, behaving like normal browser submission is safer and more predictable.

Use the form, not a fake submit system

I prefer keeping real form behaviour:

<form>

and enhancing it with JavaScript. Then methods such as:

requestSubmit()

fit into the platform instead of rebuilding submission from scratch. Use requestSubmit() for normal interface actions. Reserve submit() for code that deliberately needs to skip validation and submit handlers.

Want normal user-like form submission?
→ requestSubmit()

Want direct low-level form submission?
→ submit()

Published on and filed under and .