Advance JavaScript (DOM, BOM, Web API) / Document Object Model

Event Handling - Basic

βœ… What Is an Event?

An event is an action that happens in the browser:

  • A user clicks a button

  • A key is pressed

  • A page loads

  • The user scrolls

πŸ‘‰ JavaScript can detect these actions and respond to them.


βœ… Event Flow (Event Models)

When a user interacts with a webpage (click, keypress, submit, etc.), the event doesn’t just fire on one element.

It travels through the DOM tree in a fixed order, which is called Event Flow

Event flow defines how an event propagates (travels) through the DOM hierarchy.

🧩 Visual DOM Structure Example


πŸ”΅ 1. Event Capturing Phase (Trickling Down)

πŸ“Œ What happens?

  • The event starts from the top-most element (document)

  • Travels downward toward the target element

  • Also called event trickling

Document β†’ HTML β†’ Body β†’ Div β†’ Button

πŸ”Ή Syntax

element.addEventListener("click", handler, true);

πŸ‘‰ The third argument true enables capturing mode

βœ… Capturing Example

πŸ“Œ Why this order?

Because the event moves from parent β†’ child

🧠 When is Capturing Useful?

  • Security validations

  • Intercepting events before they reach the target

  • Complex UI frameworks

  • Closing Modal or Menu

  • Preventing certain interactions early

⚠️ Used rarely in normal applications


🎯 2. Target Phase

πŸ“Œ What happens?

  • The event reaches the target element

  • Both capturing and bubbling listeners can run here

πŸ“Œ The target phase is unavoidable


🟒 3. Event Bubbling Phase (Default Behavior)

πŸ“Œ What happens?

  • The event starts from the target element

  • Travels upward through parent elements

  • This is the default event behavior

Button β†’ Div β†’ Body β†’ HTML β†’ Document

βœ… Bubbling Example

πŸ“Œ Why this order?

Because the event bubbles from child β†’ parent


πŸš€ Event Delegation (Powered by Bubbling)

πŸ“Œ What is Event Delegation?

Instead of adding event listeners to many child elements,

add one listener to a parent and handle events using bubbling.

βœ… HTML

<ul id="list">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

πŸ’‘ Benefits

βœ” Better performance

βœ” Works with dynamically added elements

βœ” Cleaner code


β›” Stopping Event Flow

πŸ”Ή event.stopPropagation()

Stops the event from moving further up or down

Only button clicked

πŸ“Š Capturing vs Bubbling

Feature

Capturing

Bubbling

Direction

Top β†’ Down

Bottom β†’ Up

Default

❌ No

βœ… Yes

Common Usage

Rare

Very Common

Used For

Early interception

Event delegation


βœ… Ways to Handle Events

1️⃣ Inline HTML Event (❌ Avoid)

<button onclick="alert('Clicked')">Click</button>

❌ Mixes HTML and JS

❌ Hard to maintain

2️⃣ DOM Property Method

const btn = document.querySelector('button');

btn.onclick = () => {
  alert("Clicked");
};

⚠️ Only one handler allowed

New assignment overwrites old one

3️⃣ addEventListener() (βœ… Best Practice)

btn.addEventListener("click", () => {
  alert("Clicked");
});

βœ”οΈ Multiple handlers allowed

βœ”οΈ Supports bubbling & capturing

βœ”οΈ Clean separation of logic