The code below will call the function on every element inside the body. But I only want it for body element.
const bodyElement = document.querySelector("body");
bodyElement.addEventListener("click", function() {
console.log("hello");
});
The code below will call the function on every element inside the body. But I only want it for body element.
const bodyElement = document.querySelector("body");
bodyElement.addEventListener("click", function() {
console.log("hello");
});
The first argument to the event handler will be an Event object.
It has a target
property that will tell you the element clicked on.
Compare it to bodyElement
.
Try to use this:
e.stopPropagation()
If I understand correctly, you want to use this only for one element, so...