Why 'this' works as expected in this context
const p = document.querySelectorAll('p')
function changeColor() {
this.style.backgroundColor = 'red'
}
p.forEach(element => element.addEventListener('click', changeColor)
But fails in this other?
const p = document.querySelectorAll('p')
const changeColorArrow = () => this.style.backgroundColor = 'red'
p.forEach(element => element.addEventListener('click', changeColorArrow)
I know the 'this' keyword works differently in arrow functions and traditional functions, but as a beginner, I can't see where exactly it fails.
Is it ok to work with the first function (changeColor) or is there a smarter way to make the arrow function (changeColorArrow) work in this case?
Thank you!