Browser
Event Handling
Events are actions or occurrences that happen in the browser, such as clicks, key presses, or form submissions. JavaScript provides various ways to handle these events to create interactive web applications.
Adding Event Listeners
The addEventListener()
method attaches an event handler to an element.
This method provides several advantages:
Multiple event listeners can be attached to a single element.
It allows removing event listeners later.
It provides better separation of JavaScript from HTML.
Common Event Types
Events can be triggered by user actions or system interactions.
Mouse Events
click
– Triggered when an element is clicked.dblclick
– Triggered on a double-click.mouseover
– Triggered when the mouse enters an element.mouseout
– Triggered when the mouse leaves an element.mousedown
/mouseup
– Triggered when a mouse button is pressed or released.
Example:
Keyboard Events
keydown
– Triggered when a key is pressed down.keyup
– Triggered when a key is released.keypress
– (Deprecated) Used for detecting character key presses.
Example:
Form Events
submit
– Triggered when a form is submitted.change
– Triggered when the value of an input field changes.focus
/blur
– Triggered when an input field gains or loses focus.
Example:
Window Events
load
– Triggered when the page finishes loading.resize
– Triggered when the window is resized.scroll
– Triggered when the page is scrolled.
Example:
Event Object (event
)
When an event occurs, an event
object is automatically passed to the event handler, containing useful information about the event.
Example:
Event Propagation (Bubbling and Capturing)
JavaScript events propagate through the DOM in two phases:
Capturing Phase – The event moves from the root element down to the target.
Bubbling Phase – The event moves from the target back up to the root.
By default, events bubble up.
Example of event bubbling:
Clicking the child element will log:
To stop event propagation:
To use the capturing phase, pass true
as the third argument in addEventListener()
:
Removing Event Listeners
Use removeEventListener()
to detach an event handler.
Important: The function reference must match exactly for removeEventListener()
to work.
Event Delegation
Instead of adding event listeners to multiple elements, event delegation attaches a single listener to a parent element and checks which child triggered the event.
Benefits of event delegation:
Improves performance by reducing the number of event listeners.
Useful for dynamically added elements.
Preventing Default Behavior
Some events have default browser behavior (e.g., form submissions, link clicks). The event.preventDefault()
method prevents this.
Conclusion
Event handling is essential for interactive web applications. This section covered event listeners, event propagation, event delegation, and preventing default behaviors. The next section will focus on working with forms and user input in JavaScript.
Join our Community Forum
Any other questions? Get in touch