⬅ Previous Topic
JavaScript Form Validation - Validate User Input with Pure JSNext Topic ⮕
Creating Interactive UI Elements with JavaScript⬅ Previous Topic
JavaScript Form Validation - Validate User Input with Pure JSNext Topic ⮕
Creating Interactive UI Elements with JavaScriptIn JavaScript, DOM events are actions that occur in the browser, such as a user clicking a button, pressing a key, or moving the mouse. These events can be listened to and handled using addEventListener
.
Everything in JavaScript happens inside the browser environment through these events. You write functions that respond to those events — known as event handlers.
addEventListener
You can attach a listener to any DOM element that listens for a specific event like click
, keydown
, mouseenter
, etc.
// Select an element by ID
const button = document.getElementById('myButton');
// Add click event listener
button.addEventListener('click', function () {
console.log('Button was clicked!');
});
Button was clicked!
When the user clicks on the button, the callback function inside addEventListener
is triggered and logs the message.
Imagine you have a long list of items, and you want to handle clicks on each of them. Attaching individual listeners to each item would be inefficient.
Event delegation solves this by attaching a single listener to a common parent. The event bubbles up from the child to the parent, and we handle it at the parent level.
// Parent container
const parent = document.getElementById('listContainer');
// Add one listener to the parent
parent.addEventListener('click', function (event) {
if (event.target.tagName === 'LI') {
console.log('Clicked item:', event.target.textContent);
}
});
Clicked item: Item 3
Every time you click on a list item, the event bubbles up to the parent #listContainer
. The parent handles the event and checks if the original click was on an LI
tag using event.target
.
What would happen if you clicked on whitespace in the parent that is not part of a child LI
?
Answer: Nothing would be logged because the condition event.target.tagName === 'LI'
would be false.
Event delegation is commonly used in dynamic lists, menus, or UI elements generated at runtime — where attaching listeners to each element is either costly or impossible ahead of time.
// Wrapper that contains multiple buttons
const wrapper = document.getElementById('buttonWrapper');
// Single event listener for all buttons
wrapper.addEventListener('click', function (e) {
if (e.target.tagName === 'BUTTON') {
console.log('You clicked:', e.target.textContent);
}
});
You clicked: Save
event.target
to determine what was clicked.event.currentTarget
to refer to the element the listener is bound to.tagName
or use class checks to avoid unwanted triggers.event.target
— which can lead to errors when parent or whitespace is clicked.⬅ Previous Topic
JavaScript Form Validation - Validate User Input with Pure JSNext Topic ⮕
Creating Interactive UI Elements with JavaScriptYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.