# Events & Listeners

> Make your website interactive by listening to user actions. Learn how to capture clicks, keystrokes, and the powerful Event Object.

Subject: JavaScript | Lesson 43 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/events-and-listeners
Previous: [DOM Manipulation](https://codewithanimation.in/javascript/dom-manipulation.md) | Next: [Bubbling, Capturing & Delegation](https://codewithanimation.in/javascript/event-bubbling-delegation.md)

## Waiting for the User

A webpage is static until the user does something: clicking a button, scrolling the page, or typing in a field.

The browser broadcasts these actions as **Events**. As a developer, you can write JavaScript functions that "listen" for these events and execute code in response.

## addEventListener

The modern standard for capturing events is the `addEventListener()` method.

It takes two primary arguments: the type of event you are listening for (like `'click'`), and the callback function to run when it happens: `button.addEventListener('click', () => { console.log('Clicked!'); });`

## The Event Object

When an event fires, the browser doesn't just run your function; it passes a massive object full of data into your function. This is the **Event Object**.

If you define a parameter in your callback (usually named `e` or `event`), you can access this data:

- `e.type`: The type of event (e.g., 'click').

- `e.target`: The exact DOM element the user interacted with.

- `e.clientX` / `e.clientY`: The exact pixel coordinates of the mouse.

## Common mistake: Default Behaviors

Many HTML elements have built-in behaviors. If you click an `<a>` link, the browser tries to navigate to a new page. If you hit enter in a `<form>`, the browser tries to refresh the page to submit it.

If you are building a modern Single Page Application (SPA), you usually want to stop this using `e.preventDefault()`. This tells the browser: "Stop your default action, my JavaScript will handle it instead."

## Tip: Cleanup is Mandatory

If you add an event listener to an element that gets deleted from the page, the listener can get stuck in memory, causing a memory leak. If you are destroying elements, always call `removeEventListener()` first.

## Try it: The Event Inspector

Interact with the Target Pad below (click, double-click, or hover). Watch how the browser instantly generates a detailed Event Object containing data about your specific interaction.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/events-and-listeners

## Check yourself

### Question 1

What is the purpose of `e.preventDefault()`?

- A. It stops the browser from executing its built-in default behavior for that element.
- B. It stops the event from bubbling up the DOM tree.
- C. It deletes the event listener from memory.

Correct answer: A

For example, calling `e.preventDefault()` on a form submit event stops the page from refreshing, allowing your JS to handle the data via an API call.

- Why not B: Incorrect. That is what `e.stopPropagation()` does.
- Why not C: Incorrect. That requires `removeEventListener()`.

### Question 2

Where does the Event object come from?

- A. The browser automatically generates it and passes it as the first argument to your callback function.
- B. You have to create it manually using `new Event()`.
- C. It is imported from a JavaScript library.

Correct answer: A

The browser engine detects the hardware interaction (mouse click, keyboard press) and constructs the Event object before firing your listener.

- Why not B: Incorrect. While you can create custom events, standard interaction events are generated by the browser.
- Why not C: Incorrect. It is a native part of the Web API.

### Question 3

What property of the Event object tells you exactly which HTML element the user clicked?

- A. e.target
- B. e.element
- C. e.type

Correct answer: A

`e.target` holds a reference to the exact DOM node that triggered the event, which is incredibly useful for dynamic interfaces.

- Why not B: Incorrect. There is no 'element' property on the event object.
- Why not C: Incorrect. 'type' tells you the kind of event (like 'click' or 'keydown').

## Remember this

- Use `addEventListener()` to attach JavaScript logic to user interactions.
- The browser automatically passes an `Event` object to your callback function.
- `e.target` gives you the exact DOM element that fired the event.
- `e.preventDefault()` stops built-in HTML behaviors (like links navigating or forms submitting).
- Failing to use `removeEventListener()` on deleted nodes causes memory leaks.
