Event Propagation in JavaScript: Capturing, Target, Bubbling

When you click an element in the DOM, the event doesn’t just stay there — it travels through three important phases: 1️⃣ Capturing Phase The event starts from the top of the DOM tree (window → html → body → ancestors) and travels down to the target element. 2️⃣ Target Phase The event reaches the actual element that was clicked. 3️⃣ Bubbling Phase After execution, the event travels back up the DOM (target → parent → ancestors → window). 💡 By default, addEventListener() listens in the bubbling phase. 🛑 We can control propagation using: event.stopPropagation() → Stops the event from moving to parent elements. event.stopImmediatePropagation() → Stops other handlers on the same element + prevents further bubbling. Understanding this concept is key to mastering Event Delegation, performance optimisation, and clean DOM event handling. #JavaScript #WebDevelopment #Frontend #EventPropagation #DOM #LearningInPublic

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories