The onsubmit attribute is an event-handler attribute for <form>. It runs JavaScript when the submit event fires — the form is being submitted (submit button, Enter for implicit submit, or form.requestSubmit()).
The event fires on the form, not on the button. Use event.preventDefault() (or return false in an inline handler) to stop the default navigation. Related: onreset, action, method, and required.
Baseline · Widely available.
Not deprecated and not experimental. The form submit event is stable across modern browsers since about July 2015.
Prefer addEventListener in apps.
Inline onsubmit is fine for learning. Production code usually uses form.addEventListener("submit", handler). Note: form.submit() does not fire this event — use requestSubmit() when you want validation + submit.
Syntax
HTML Syntax
syntax.html
<form onsubmit="script">
script is JavaScript that runs on submit. Equivalent JS: form.onsubmit = handler or form.addEventListener("submit", handler).
Values
Attribute Values
The attribute value is always a script. Common patterns:
required / min — browser blocks empty/invalid values first.
When valid, submit fires — your handler runs.
Custom rule — e.g. age ≥ 18 — after built-in checks.
Keep or cancel — here we always preventDefault for the demo.
Edge Cases & Pitfalls
Most confusion is form.submit() skipping the event, or putting the handler on the button.
form only
Event targets the form
Listen on <form>. The button triggers submission; the submit event still fires on the form. SubmitEvent.submitter names the button when available.
.submit()
form.submit() skips submit
A direct submit() call does not fire the event. Prefer requestSubmit() when you want handlers and HTML validation to run.
validation
Invalid forms do not submit
Failed constraint validation blocks submission — no submit event. See required and novalidate.
vs reset
onsubmit ≠ onreset
onreset restores defaults. onsubmit handles sending. Separate buttons: type="submit" vs type="reset".
property
.onsubmit = replaces listeners
Assigning the IDL property overwrites a previous assignment. Prefer addEventListener for multiple handlers.
Compatibility
Browser Support
onsubmit is an HTML form event-handler attribute for the submit event. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
onsubmit
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the form submit event.
97%Global support (approx.)
Google Chrome1+
Full support
Mozilla Firefox1+
Full support
Apple Safari1+
Full support
Microsoft Edge12+
Full support
Opera4+
Full support
Samsung InternetModern · Full
Full support
Internet Explorer4+
Supported
onsubmitWidely supported
Bottom line: Safe to use for form handling. Prefer addEventListener and preventDefault in apps; use requestSubmit() when scripting a submit that should validate and fire the event.
Remember
Key Takeaways
Role: run script when a form is submitted.
Cancel:preventDefault() or inline return false.
Script submit: use requestSubmit(), not bare submit(), to fire the event.
Validation: invalid fields block submit until fixed.
One line:onsubmit means “the form is submitting” — handle it on <form>, cancel with preventDefault when you take over in JavaScript.
Frequently Asked Questions
It is an event-handler attribute on a form. When the form is submitted — submit button, Enter (implicit submit), or requestSubmit() — the submit event fires and the script runs.
On the <form> element itself, not on the submit button. The submit event fires on the form.
Call event.preventDefault() in the handler, or return false from an inline onsubmit handler. That cancels the default submit (navigation / HTTP request).
No. Calling form.submit() directly submits without firing the submit event. Use form.requestSubmit() if you want the submit event and validation to run.
Invalid controls block submission. You get invalid feedback instead of a submit event until the form is valid (unless you use novalidate).
No. The submit event and onsubmit handler are Baseline widely available. Prefer addEventListener in production apps.