The browser
event.preventDefault(): cancelling the browser's action
"Thanks, browser, I'll handle this one."
Why it exists
The browser does a lot by itself: follows links, submits forms, ticks checkboxes, scrolls on the space bar. That's handy until you need something else: validate a form before sending and show an error, open the cart in a popup without a reload, make the space bar a shortcut of your own. preventDefault() lets you say "stop" to the browser's action without getting in the way of the event itself.
Where it's used
- Forms: submit →
preventDefault(), validate the fields, send the data from code without reloading the page. - Links in single-page apps: the router intercepts the click and switches the page itself, without loading new HTML.
- Keyboard shortcuts:
Ctrl+Ssaves the document in the app, not the browser's page. - Dragging files in: without
preventDefault()ondragover/drop, the browser just opens the file.
Syntax
link.addEventListener("click", (event) => {
event.preventDefault();
// do it your own way
});
event.defaultPrevented; // true if the action was cancelledExamples
const link = new EventTarget();
link.addEventListener("click", (event) => {
event.preventDefault();
});
// dispatchEvent returns false if the default action was cancelled
const allowed = link.dispatchEvent(new Event("click", { cancelable: true }));
allowed; // → falseconst form = new EventTarget();
const sent = [];
form.addEventListener("submit", (event) => {
event.preventDefault(); // the page won't reload
sent.push("order: 2 mice");
});
const reload = form.dispatchEvent(new Event("submit", { cancelable: true }));
[reload, sent]; // → [false, ["order: 2 mice"]]const page = new EventTarget();
let prevented;
page.addEventListener("scroll", (event) => {
event.preventDefault();
prevented = event.defaultPrevented;
});
page.dispatchEvent(new Event("scroll")); // not a cancelable event
prevented; // → falsepreventDefault() does nothingResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
An event first goes through all the handlers (capture, target, bubble), and only then does the browser perform the default action, unless someone called preventDefault(). The method sets the event.defaultPrevented = true flag, and in that case dispatchEvent returns false.
Not everything can be cancelled: the event has to be cancelable: true. Clicks, submit and keydown are cancelable; scroll isn't (the scrolling has already happened, the event just reports it). In handlers with the { passive: true } option (often set on touchstart and wheel for smooth scrolling), preventDefault() is ignored.
Don't confuse it with stopPropagation(): that one stops the event's trip through the tree but doesn't cancel the browser's action. They're two independent levers.
Traps
When to pick something else
If you don't need to replace the browser's behaviour, don't cancel it. A link that goes to another page should stay a link: that's what makes "open in new tab", bookmarks and screen readers work. A button that doesn't submit anything is better off with type="button" than with submit muted by preventDefault().