In JavaScript, when an event occurs (like a click, key press, or mouse movement), the browser creates an Event Object that contains details about that event. This object is automatically passed to the event handler function and allows you to respond dynamically based on the event's specifics.
💡 What is the Event Object?
The Event Object holds information such as the event type, the target element, mouse coordinates, key pressed, and more — all depending on the kind of event triggered.
Common Properties of the Event Object
type: The type of event (e.g., "click", "keydown").target: The DOM element that triggered the event.currentTarget: The element the event listener is attached to.preventDefault(): A method to stop the default browser action.stopPropagation(): A method to stop event bubbling up the DOM tree.clientXandclientY: Mouse coordinates relative to the viewport.key: The key value from keyboard events.
| Property/Method | Description |
|---|---|
| type | Type of the event (e.g., "click", "input") |
| target | Element that initiated the event |
| currentTarget | Element currently handling the event |
| preventDefault() | Prevents default browser behavior |
| stopPropagation() | Stops event from bubbling/capturing |
| clientX, clientY | Mouse position relative to viewport |
| key | Key pressed during keyboard event |
Using the Event Object in an Event Listener
When adding an event listener, include a parameter to receive the event object:
📌 Deep Dive: Accessing the Event Object
button.addEventListener('click', function(event) {
console.log('Clicked element:', event.target);
console.log('Event type:', event.type);
});
Here, the event parameter gives access to details about the click event, such as which element was clicked.
💡 Event Parameter Name
You can name the event parameter anything you like (commonly e or event), but it must be the first argument to receive the event object.
Preventing Default Behavior
Some events have default actions (like submitting a form or following a link). You can prevent these defaults using event.preventDefault().
📌 Deep Dive: Preventing Default Form Submission
form.addEventListener('submit', function(event) {
event.preventDefault(); // Stops form from submitting
alert('Form submission prevented!');
});
Stopping Event Propagation
Events bubble up through parent elements by default. Use event.stopPropagation() to stop this behavior when needed.
📌 Deep Dive: Stopping Event Bubbling
childElement.addEventListener('click', function(event) {
event.stopPropagation(); // Prevents parent handlers from running
console.log('Child clicked');
});
parentElement.addEventListener('click', function() {
console.log('Parent clicked');
});
⚠️ Remember
Not all events support every property or method. Check the event type you are handling and test accordingly.
Summary
- The Event Object provides essential details about user interactions.
- It is passed automatically to event handlers as the first argument.
- Commonly used properties include
type,target, and methods likepreventDefault(). - Controlling event flow and default actions helps create interactive, user-friendly interfaces.
Quick Knowledge Check
Test what you just learned
Question 1 of 2
What does the event.target property represent?
Question 2 of 2
Which method prevents the default browser action for an event?
Loading results...
