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>
Example
#<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>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
#<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 asevent.preventDefault().this.elements
The form’s fields. A field withname="email"is read asthis.elements.email.value.new FormData(this)
The values of all fields with anameattribute, ready to be sent withfetch().event.submitter
The button that submitted the form. Useful when the form has several buttons, for example “Save draft” and “Publish”.
More examples
#<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>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
submitevent on the form, not with a button click.submitalso 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
#- <form>A form that collects data from the user and sends it to a given address.
- <button>A clickable button: submits a form, clears it, or runs an action on the page.
- <input>A form field: text, email, password, number, date, slider, checkbox, file and many more.
- onchangeRuns JavaScript code when the user commits a new field value: picks an option, ticks a checkbox or finishes typing.
- onclickRuns JavaScript code when an element is clicked. The simplest, but not the best way to handle events.
Spotted a mistake or missing an example? Write to us.