HTML global attributeGlobal attributes

onsubmit

Runs JavaScript code right before a form is submitted. Lets you stop the submission and handle the data in a script.

Applies to
<form>
On this page

Example

#
HTML
<form onsubmit="
  event.preventDefault();
  const email = this.elements.email.value;
  this.hidden = true;
  document.getElementById('thanks').textContent =
    'Thanks! We sent a link to the first lesson to ' + email + '.';
">
  <label for="email">Email</label>
  <input type="email" id="email" name="email" placeholder="anna@example.com" required>
  <button>Subscribe to the newsletter</button>
</form>
<p id="thanks"></p>
Preview

First try submitting the empty form: the browser shows a validation error, and the onsubmit code does not run. After you enter a valid address, the form disappears and a thank-you message appears.

Definition and usage

#

You put the onsubmit attribute on a <form> element. The submit event fires when the user submits the form: clicks the submit button or presses Enter in a text field. Before that, the browser checks validation rules such as required or type="email", and if any field is invalid, the event does not fire at all.

Most often event.preventDefault() is called in it. This stops the ordinary submission and page reload, so you can send the data yourself, for example with fetch(), and show the result without leaving the page. In the attribute, return false gives the same effect.

In the attribute’s code, this points to the form. You read the fields through this.elements, and all values at once through new FormData(this). Remember that the form.submit() method called in a script submits the form without a submit event and without validation. If you want both to work, use form.requestSubmit(). In larger projects, add the handling through addEventListener("submit", …).

Syntax

#
Syntax
<form onsubmit="JavaScript code">…</form>

Available in the attribute’s code

#
  • event.preventDefault()

    Stops the ordinary form submission and page reload.
  • return false

    A shortcut that works only in an attribute. It has the same effect as event.preventDefault().
  • this.elements

    The form’s fields. A field with name="email" is read as this.elements.email.value.
  • new FormData(this)

    The values of all fields with a name attribute, ready to be sent with fetch().
  • event.submitter

    The button that submitted the form. Useful when the form has several buttons, for example “Save draft” and “Publish”.

More examples

#
Handling submission with addEventListener and FormData
HTML
<form id="signup">
  <label>Name <input name="name" required></label>
  <label>Course
    <select name="course">
      <option>HTML and CSS</option>
      <option>JavaScript</option>
      <option>Python</option>
    </select>
  </label>
  <button>Sign me up</button>
</form>
<p id="result"></p>

<script>
  document.getElementById('signup').addEventListener('submit', (event) => {
    event.preventDefault();
    const data = new FormData(event.currentTarget);
    document.getElementById('result').textContent =
      data.get('name') + ', we signed you up for the ' + data.get('course') + ' course.';
  });
</script>
Preview

In a real application the data would go to a server, for example through fetch("/api/zapisy", { method: "POST", body: data }), not into a paragraph on the page.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Handle submission with the submit event on the form, not with a button click. submit also catches submission with the Enter key and runs only after successful validation.
  • After event.preventDefault(), feedback is up to you: show that the data is being sent, disable the button against double clicks, and display the result or an error.

Related entries

#

Spotted a mistake or missing an example? Write to us.