How to get the element clicked for the whole document with JavaScript?

Spread the love

Sometimes, we want to get the element clicked for the whole document with JavaScript.

In this article, we’ll look at how to get the element clicked for the whole document with JavaScript.

How to get the element clicked for the whole document with JavaScript?

To get the element clicked for the whole document with JavaScript, we use the e.target property.

For instance, we write

document.addEventListener("click", (e) => {
  const target = e.target;
});

to add a click listener to the document with addEventListener.

Then we get the element that’s clicked with e.target.

Conclusion

To get the element clicked for the whole document with JavaScript, we use the e.target property.

Leave a Reply

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