Skip to content
Advertisement

How to deal with onClick event?

For instance, I have li. Inside I have paragraphs, spans…

<ul>
  <li>
    <p>ABC</p>
    <span>ABC</span>
  </li>
</ul>

When I try to manage with onClick event I notice that target elements are sometimes a paragraph, sometimes a span. But I certainly need a li as a target element when I click on it or on tags inside it.

Advertisement

Answer

Use event.currentTarget instead of event.target – it always refers to the element you actually attached the listener to, not the element that the Event originally triggered on.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement