About Lesson
Preventing Default and Propagation
- .preventDefault(): Prevents the default action of an element (e.g., following a link).
- .stopPropagation(): Stops the event from bubbling up to parent elements.
JavaScript
$("a").click(function (e) {
e.preventDefault();
alert("Default action prevented!");
});
$(".child").click(function (e) {
e.stopPropagation();
alert("Child clicked without propagating to parent!");
});