I am not sure of my understanding upon differences between event.target and event.currentTarget.
This is how I explain in my words: In bubbling phase, one element that has no event attached, can trigger an event that is attached to an upper element. First is called target and second currentTarget.
Is it a relevant definition? Is there more to say/understand?
Hey @yruka, yes that’s correct understanding, e.currentTarget always refer to this (element you have added the listener to). It might be useful in case when you use fat arrow functions as your callback, other times you can just use this:
// using regular function
function handleFunction(e) {
console.log(this === e.currentTarget); // always true
}
document.addEventListener('click', handleFunction);
// using fat arrow function
// fat arrow functions do not have `this` variable, therefore e.currentTarget must be used
const handleFatArrowFunction = (e) => {
console.log(e.currentTarget.id); // same as this.id in regular function
};
document.body.addEventListener('click', handleFatArrowFunction);