Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / JavaScript Tutorial / Chapter 4: DOM Manipulation & Events
JAVASCRIPT ⏱ 12 min read

Events and Event Listeners

Events are signals fired by the browser when things happen—like user clicks, key presses, scrolling, or form submissions.

What is it?

The `addEventListener()` method attaches an event handler to an element without overwriting existing event handlers.

Why is it used?

Powers all user interaction: handling button clicks, validating keyboard keystrokes, intercepting form submissions, and debouncing search inputs.

Detailed Explanation

Common Browser Events: • Mouse: `click`, `dblclick`, `mouseenter`, `mouseleave` • Keyboard: `keydown`, `keyup` (read `event.key` for 'Enter', 'Escape') • Form: `submit`, `input` (fires on every keystroke), `change` (fires on blur/select) • Window: `DOMContentLoaded` (HTML parsed), `scroll`, `resize` The Event Object (`e` or `event`): Passed automatically into callback functions. Essential methods: • `e.target`: The exact element that triggered the event. • `e.preventDefault()`: Prevents browser default behavior (e.g. stopping form from reloading page).

Syntax Structure

Syntax Blueprint
element.addEventListener('click', (event) => {
  console.log('Clicked!', event.target);
});

Code Example

JAVASCRIPT Code
const searchInput = document.getElementById("tutorialSearchInput");
const courseCards = document.querySelectorAll(".tutorial-card");

// Filter cards in real-time on keyboard input
searchInput.addEventListener("input", (e) => {
  const query = e.target.value.toLowerCase().trim();

  courseCards.forEach(card => {
    const title = card.getAttribute("data-title") || "";
    const desc = card.getAttribute("data-desc") || "";

    if (!query || title.includes(query) || desc.includes(query)) {
      card.style.display = "flex";
    } else {
      card.style.display = "none";
    }
  });
});

Explanation of the Code

• Listens to the `input` event on the search box. • Reads typed query via `e.target.value`. • Dynamically shows or hides course cards in real time without refreshing.

Expected Output

Typing in the search input immediately filters cards matching the entered keywords.

Real-World Application

Used across the redesigned IGM Computers tutorial catalog for instant client-side lesson and course searching.

Common Mistakes to Avoid

• Forgetting `e.preventDefault()` inside a form `submit` listener, causing the page to reload instantly and lose application state. • Using inline HTML event attributes (`onclick="..."`) which creates messy, unmaintainable code.

Quick Practice Challenge

Attach a `keydown` listener to the window that closes an open modal when the user presses the 'Escape' key.

Key Takeaways

• Use `addEventListener('event', callback)` for all event handling. • Use `e.preventDefault()` to stop form reloads and link redirects. • Use `input` event for instantaneous real-time search filtering.