The browser
Forms and the submit event
Submitting a form is a submit event on the <form>; preventDefault() cancels the page reload.
Why it exists
Almost everything a person tells a website goes through a form: logging in, signing up, ordering, searching, commenting. Without JavaScript a form can only send the data and show the server's response as a new page — errors appear only after a reload, and what was typed is often lost. A submit handler lets you intercept the submission: check the fields, show clear messages next to them and send the data without a reload. And <form> itself gives you Enter-to-submit, autofill and screen reader support for free.
Where it's used
- Sign-up and login: check the email and password before sending, show "Passwords don't match" right under the field.
- Checkout: address, phone, delivery method — with each field checked.
- Site search: a one-field form where Enter runs the search without a reload.
- Comments and reviews on social networks and shops: posting without leaving the page.
Syntax
form.addEventListener("submit", (event) => {
event.preventDefault(); // don't reload the page
// read the fields, check them, show errors
});Examples
const form = document.querySelector("#entry");
form.addEventListener("submit", (event) => {
event.preventDefault();
const name = form.querySelector("#name").value.trim();
if (name === "") {
form.querySelector("#name-error").textContent = "Enter a name";
return;
}
console.log("Entry from", name);
});function validate(values) {
const errors = {};
if (values.name.trim() === "") errors.name = "Enter a name";
if (!/^\S+@\S+\.\S+$/.test(values.email)) errors.email = "That doesn't look like an email";
return errors;
}
validate({ name: " ", email: "byte@tasky.dev" }); // → { name: "Enter a name" }
validate({ name: "Byte", email: "byte" }); // → { email: "That doesn't look like an email" }
validate({ name: "Byte", email: "byte@tasky.dev" }); // → {}const form = new EventTarget(); // in a browser this would be a <form>
form.addEventListener("submit", (event) => event.preventDefault());
const event = new Event("submit", { cancelable: true });
form.dispatchEvent(event); // → false — the default action was cancelled
event.defaultPrevented; // → truepreventDefault() marks the event, and the browser skips its own actionconst query = new URLSearchParams({ q: "ginger cat", page: "2" });
query.toString(); // → "q=ginger+cat&page=2"method="get" would write its data into the page addressResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
Forms appeared in HTML before JavaScript. Without scripts, on submission the browser collects name=value pairs from the fields that have a name attribute, sends them to the address in action (the current one by default) using method (by default GET, which puts the data in the address: ?name=Whiskers), and shows the response as a new page. That's the "reload".
A submission is started by a button inside the form (a <button> in a form is type="submit" by default) or by Enter in a text field. Before sending, the browser checks the built-in rules — required, pattern, type="email", minlength — and if something's wrong it shows its own hint and doesn't send. The novalidate attribute on the form turns that check off when you do the checking yourself.
If the check passes, the form fires a submit event. The handlers run first, then the default action. event.preventDefault() cancels it: the page stays, and the data can be sent with fetch.
All of this runs on the user's computer, so the server must check the data again: attributes can be deleted in DevTools, and a request can be sent without any page at all.
Traps
How it differs from…
When to pick something else
When the input doesn't need to be "sent": filtering a list as you type, a volume slider, a theme switch. The input or change events on the field itself are enough there. But if there's a "Send", "Log in" or "Search" button — use <form> and submit, not a <div> with a click handler.