preventDefault and stopPropagation
Control default behavior and bubbling.
preventDefault and stopPropagation is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Controlling Event Behavior
Two mechanisms let you control events: stopping the browser’s default action and stopping the event from traveling through the DOM.
Default Actions
Many elements have built-in default behaviors: links navigate, form submits reload, checkboxes toggle. These happen unless you intervene.
preventDefault
event.preventDefault cancels the browser’s default action while still letting your handler run.
link.addEventListener("click", (e) => {
e.preventDefault(); // stop navigation
console.log("handled in JS");
});preventDefault on Forms
A common use is stopping a form from reloading the page so you can handle submission with JavaScript.
form.addEventListener("submit", (e) => {
e.preventDefault();
// send data via fetch instead
});Event Propagation
After hitting the target, events bubble upward through ancestors, triggering their listeners. This is why a click on a child can fire a parent’s handler.
stopPropagation
event.stopPropagation prevents the event from continuing to ancestors. The current handler still finishes.
child.addEventListener("click", (e) => {
e.stopPropagation(); // parent listeners will not fire
});preventDefault vs stopPropagation
They are independent. preventDefault cancels the browser action; stopPropagation cancels travel to other elements. You may use either, both, or neither.
e.preventDefault(); // no default action
e.stopPropagation(); // no bubblingOther Listeners on the Same Element
stopPropagation does NOT stop other listeners on the same element from running. They still fire.
el.addEventListener("click", (e) => e.stopPropagation());
el.addEventListener("click", () => console.log("still runs"));stopImmediatePropagation
stopImmediatePropagation goes further: it stops bubbling AND prevents any remaining listeners on the same element.
el.addEventListener("click", (e) => e.stopImmediatePropagation());
el.addEventListener("click", () => console.log("does NOT run"));defaultPrevented
You can check event.defaultPrevented to see whether preventDefault was already called by an earlier handler.
el.addEventListener("click", (e) => {
console.log(e.defaultPrevented);
});Use Sparingly
Stopping propagation can break event delegation elsewhere on the page. Prefer it only when truly necessary and consider delegation-friendly alternatives.
Quick Check
Pick the right method.
Recap
preventDefault cancels the browser’s default action (navigation, submit, etc.). stopPropagation stops the event from bubbling to ancestors, while stopImmediatePropagation also blocks other listeners on the same element. These are independent tools; use propagation-stopping sparingly to avoid breaking delegation.
Frequently asked questions
Is the “preventDefault and stopPropagation” lesson free?
Yes — the full text of “preventDefault and stopPropagation” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “preventDefault and stopPropagation”?
Control default behavior and bubbling. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “preventDefault and stopPropagation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- addEventListener Basics
- The Event Object
- preventDefault and stopPropagation
- Keyboard and Mouse Events