Event Delegation
Handle events on parent element
JavaScript Event Delegation Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Basic event delegation
const list = document.getElementById('myList');
// Instead of adding listener to each item
list.addEventListener('click', function(e) {
if (e.target.tagName === 'LI') {
console.log('List item clicked:', e.target.textContent);
e.target.classList.toggle('active');
}
});
// Method 2: Dynamic content
const container = document.getElementById('container');
container.addEventListener('click', function(e) {
if (e.target.classList.contains('button')) {
console.log('Button clicked:', e.target.id);
}
});
// Add new buttons dynamically - they work automatically!
function addButton(text) {
const btn = document.createElement('button');
btn.className = 'button';
btn.textContent = text;
container.appendChild(btn);
}
// Method 3: Multiple selectors
container.addEventListener('click', function(e) {
if (e.target.matches('.delete-btn')) {
deleteItem(e.target.dataset.id);
} else if (e.target.matches('.edit-btn')) {
editItem(e.target.dataset.id);
} else if (e.target.matches('.view-btn')) {
viewItem(e.target.dataset.id);
}
});
// Method 4: Closest method
container.addEventListener('click', function(e) {
const card = e.target.closest('.card');
if (card) {
console.log('Card clicked:', card.dataset.id);
}
});
// Method 5: Event delegation with data attributes
const table = document.getElementById('dataTable');
table.addEventListener('click', function(e) {
const row = e.target.closest('tr');
if (row) {
const id = row.dataset.id;
const action = e.target.dataset.action;
if (action === 'delete') {
deleteRow(id);
} else if (action === 'edit') {
editRow(id);
}
}
});// Output depends on user interaction
Understanding Event Delegation
Event delegation handles events on parent.
Benefits
How It Works
Use Cases
Best Practices
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Event Delegation
This JavaScript program is part of the "Events Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.