How to get the element that triggered the click event with JavaScript?

Spread the love

To get the element that triggered the click event with JavaScript, we use the currentTarget property.

For instance, we write

a.addEventListener("click", (e) => {
  console.log(e.currentTarget);
});

to add a click listener top the a element with addEventListener.

We set the event listener callback to a function that logs the element that triggered the click event by logging e.currentTarget.

Leave a Reply

Your email address will not be published. Required fields are marked *