AI Workshop: learn to build apps with AI →
Browser events: `preventDefault()`

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


Calling the preventDefault() method of the event object will cancel the default handling that the browser is programmed to execute.

For example: opening a new page when a link is clicked, or submitting a form when the submit event fires.

Calling preventDefault() is what you need to completely customize the action—for example, by creating a fetch request to load some JSON instead of opening a new page when a link is clicked.

Other event handlers on the same element will still execute unless you call event.stopImmediatePropagation().

Lessons in this unit:

0: Introduction
1: Handling events
2: The `DOMContentLoaded` event
3: The `event` object
4: Mouse events
5: Keyboard events
6: ▶︎ `preventDefault()`
7: Stopping event propagation
8: Bubbling and capturing
9: Form submit event
10: Input fields events
11: Creating custom events
12: Keyboard Events
13: Mouse Events
14: Touch Events
15: Form Events