Course contentsShow
← Web Development
Lesson 747 of 2,98213. DOM EventsFree lesson

Accessing the Event Object in Handlers

Learning to receive the event parameter in event handlers and the conventions around naming it.

Accessing the Event Object in Handlers

What you'll learn: How to receive and work with the event object that's automatically passed to your event handler functions.

The Automatic Parameter

When you attach an event listener to an element, the browser automatically passes information about what just happened—this is the event object. To access it, you simply add a parameter to your handler function. The browser will fill that parameter with all the event details when the event fires.

Think of it like receiving a delivery package: someone rings your doorbell (the event), and when you open the door (your handler function), they hand you a box (the event object) containing all the details about the delivery.

Naming Conventions

The most common names for this parameter are:

  • event (most descriptive)
  • evt (shorter alternative)
  • e (most concise, very common in real-world code)

All three work identically—it's purely convention. Many developers prefer e for brevity:

button.addEventListener('click', function(e) {
  console.log(e); // The event object
  console.log(e.target); // The element that was clicked
});

With arrow functions, it looks the same:

button.addEventListener('click', (e) => {
  console.log(e.type); // 'click'
});

Why This Matters

Without accepting this parameter, you can't access crucial event information like which element triggered the event, mouse coordinates, which key was pressed, or use methods like preventDefault() and stopPropagation().

Key Takeaway: The event object is automatically passed to your handler function as the first parameter—just add a parameter (commonly named e, evt, or event) to receive it and access all event details.