JavaScript Event preventDefault() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

The preventDefault() method tells the browser not to run the default action for an event—following a link, submitting a form, toggling a checkbox, and more. Learn how it pairs with cancelable and defaultPrevented, and when it has no effect—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

undefined

03

Params

None

04

Needs

cancelable

05

Sets

defaultPrevented

06

Status

Baseline widely

Introduction

Many events have a default action built into the browser. Clicking an <a href> navigates. Submitting a form sends data. Clicking a checkbox toggles it. Sometimes you want to handle the interaction yourself in JavaScript instead.

Call event.preventDefault() inside a listener. The event still bubbles (unless you stop it), but the browser skips that default behavior. If the event is not cancelable, the call does nothing.

💡
Beginner tip

preventDefault()stopPropagation(). One cancels the browser’s built-in action; the other stops the event from reaching other listeners up or down the tree.

Understanding Event.preventDefault()

An instance method that cancels the event’s default action when the event is cancelable.

  • No parameters — call event.preventDefault().
  • Return value — none (undefined).
  • Requires cancelable — if event.cancelable is false, nothing happens.
  • Sets defaultPrevented — becomes true after a successful cancel.
  • Does not stop propagation — other listeners still run unless you stop them.
  • Passive listeners — calling it may be ignored and warn in the console.
  • Baseline Widely available on MDN (since July 2015); also in Web Workers.

📝 Syntax

JavaScript
preventDefault()

Parameters

None.

Return value

None (undefined).

Typical pattern

JavaScript
link.addEventListener("click", (event) => {
  event.preventDefault();
  // Handle navigation yourself (SPA route, modal, etc.)
});

Safe pattern (check cancelable)

JavaScript
element.addEventListener("wheel", (event) => {
  if (event.cancelable) {
    event.preventDefault();
  }
});

⚡ Quick Reference

GoalCode / note
Cancel defaultevent.preventDefault()
Check firstif (event.cancelable) event.preventDefault()
Did it work?event.defaultPrevented
Synthetic cancelablenew Event("x", { cancelable: true })
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about preventDefault().

Cancels
default

Not bubbling

Needs
cancelable

Or no-op

Sets
defaultPrevented

true after

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN Event: preventDefault(). Use View Output or Try It Yourself.

📚 Getting Started

Block common UI defaults: checkbox, link, and form.

Example 1 — Block Checkbox Toggle (MDN Idea)

Clicking a checkbox normally toggles it—preventDefault stops that.

JavaScript
checkbox.addEventListener("click", (event) => {
  event.preventDefault();
  console.log("preventDefault() won't let you check this!");
});
Try It Yourself

How It Works

The click still fires your listener; the browser’s check/uncheck action is canceled.

Example 3 — Intercept Form Submit

Validate or send with fetch instead of a full page reload.

JavaScript
form.addEventListener("submit", (event) => {
  event.preventDefault();
  out.textContent = "Submit canceled — handled in JavaScript";
});
Try It Yourself

How It Works

Listen for submit (not only the button click) so Enter-key submits are covered too.

📈 Flags & Checks

Guard with cancelable; confirm with defaultPrevented.

Example 4 — Check cancelable First

Calling preventDefault on a non-cancelable event has no effect.

JavaScript
button.addEventListener("click", (event) => {
  if (event.cancelable) {
    event.preventDefault();
    out.textContent = "Canceled (cancelable was true)";
  } else {
    out.textContent = "Not cancelable — preventDefault skipped";
  }
});
Try It Yourself

How It Works

MDN recommends this guard for handlers that cover many event types (for example wheel sequences).

Example 5 — Read defaultPrevented

After a successful cancel, defaultPrevented becomes true.

JavaScript
link.addEventListener("click", (event) => {
  console.log("before:", event.defaultPrevented); // false
  event.preventDefault();
  console.log("after:", event.defaultPrevented);  // true
});
Try It Yourself

How It Works

Other listeners later in the same event can check defaultPrevented to see if someone already canceled.

🚀 Common Use Cases

  • SPA link clicks that should not reload the page.
  • Custom form submit with fetch / validation.
  • Blocking checkbox / context-menu / drag defaults when you own the UI.
  • Custom keyboard shortcuts (for example prevent browser Find on Ctrl+F in an app).
  • Teaching cancelable vs defaultPrevented for beginners.

🔧 How It Works

1

Event fires

Browser prepares a default action for this event type.

Start
2

Listener runs

You call preventDefault() while cancelable is true.

Call
3

Flag updates

defaultPrevented becomes true; event may still bubble.

State
4

Default skipped

Link does not navigate, form does not reload, and so on.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Available in Web Workers.
  • Passive listeners: preventDefault() may be ignored (and may warn).
  • Prefer HTML/CSS alternatives when they fit (disabled, validation, overflow).
  • Related learning: cancelable, defaultPrevented, returnValue (deprecated), initEvent(), dispatchEvent().

Universal Browser Support

Event.preventDefault() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is also available in Web Workers.

Baseline · Widely available

Event.preventDefault()

Cancels the browser default action when the event is cancelable.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE (prefer modern browsers)
Legacy
Event.preventDefault() Excellent

Bottom line: Use preventDefault() to cancel defaults; check cancelable and read defaultPrevented when you need certainty.

Conclusion

Event.preventDefault() cancels the browser’s default action for a cancelable event. It does not stop propagation by itself—pair it with cancelable and defaultPrevented for clear, reliable handlers.

Continue with stopImmediatePropagation(), cancelable, defaultPrevented, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Call it early in handlers that own the UX
  • Check cancelable for multi-type listeners
  • Listen to submit for forms (not only button click)
  • Keep real hrefs for accessibility when blocking nav
  • Prefer HTML attributes when they replace JS fights

❌ Don’t

  • Expect it to stop bubbling (use stopPropagation)
  • Rely on it inside passive listeners
  • Assume every synthetic event is cancelable
  • Confuse it with deprecated returnValue = false
  • Block defaults without a clear alternate UX

Key Takeaways

Knowledge Unlocked

Five things to remember about preventDefault()

Cancel defaults—not the event path.

5
Core concepts
02

Needs

cancelable

Guard
📈03

Sets

defaultPrevented

Flag
🔁04

Bubbles

still continue

Flow
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

It tells the browser not to run the event's default action—such as following a link, submitting a form, checking a checkbox, or scrolling—while the event still propagates unless you also stop propagation.
No. MDN marks Event.preventDefault() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is also available in Web Workers.
No. preventDefault() only cancels the default action. The event still travels through capture/bubble unless you call stopPropagation() or stopImmediatePropagation().
If event.cancelable is false, or if the listener is passive (common for touchmove/wheel on the document). Check cancelable first when unsure.
After a successful cancel, event.defaultPrevented is true. For synthetic events you dispatch, dispatchEvent() returns false when preventDefault was called on a cancelable event.
Sometimes yes. MDN suggests disabled/readonly on controls, HTML constraint validation, or CSS overflow to avoid fighting defaults with JavaScript when a declarative option fits.
Did you know?

For cancelable synthetic events, target.dispatchEvent(event) returns false if a listener called preventDefault()—a handy signal when you fire your own events and need to know whether they were canceled.

Next: stopImmediatePropagation()

Skip remaining same-element listeners and further path travel.

stopImmediatePropagation() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful