Facebook Pixel

How do you implement event delegation for a list?

ul.addEventListener('click', e => { if (e.target.tagName === 'LI') console.log(e.target.textContent); }). One listener on the ul, check e.target for the clicked li.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Event Delegation Interview Questions

Using event bubbling to handle events for multiple children with a single listener on the parent. The parent checks e.target to determine which child was clicked.

Memory-efficient (one listener instead of many), works for dynamically added elements (no need to add new listeners), and simpler code (one handler instead of many).

Use e.target.closest('.item') to find the relevant ancestor. For example, if a button inside a list item is clicked, closest('li') finds the list item.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.