Skip to content

Event Delegation Target Mismatch

Quick summary

You attached a listener to a parent and checked event.target — but target is the deeply nested child, not the parent you bound to.

Why this happens

  • Using event.target when you need the element the listener is attached to

  • Not checking event.target.matches(selector) before acting in delegation

  • Assuming target is always the element with the addEventListener

Minimal example

✗ Broken code
list.addEventListener('click', e => {
  console.log(e.target.id); // could be a child <span>, not the <li>
});
✓ Fixed code
list.addEventListener('click', e => {
  const li = e.target.closest('li');
  if (li) console.log(li.id); // the intended <li>
});

event.target is the deeply clicked child, not the <li>. Using closest('li') walks up to find the intended list item regardless of which child was clicked.

How to diagnose

  • Log event.target vs event.currentTarget to see the difference

  • Check whether the handler acts on target or currentTarget

  • Verify the selector match in delegated handlers

How to fix

  • Use event.currentTarget for the element with the listener

  • In delegation, check event.target.closest(selector) to find the intended element

  • Stop propagation only when you are sure you are at the right element

How to prevent

  • Understand target (origin) vs currentTarget (listener element) clearly

  • Use closest() in delegated handlers to find the relevant ancestor

Related Resources

Related Glossary

Related Lessons

  • DOM

    Learn about events.

← Back to language