I have a piece of JavaScript code that selects a bunch of nodes with the editable class and then loops over them adding an eventListener on each of them:
var elements = document.querySelectorAll('.editable');
for (const element of elements) {
element.addEventListener('mouseover',() => {
element.innerHTML += '<i class="far fa-edit"></i>'
})
}
This piece of code works fine and appends <i class="far fa-edit"></i>
in the element that is "mouseovered".
However if I replace the callback function of the evenlistener with:
() => { this.innerHTML += '<i class="far fa-edit"></i>' }
I find it weird as when I console.log this
or element
, I get a reference to the same Node!
From my understanding this
is a ref to the object on which the function is called. In that case element
, so both using this
or element
should work fine...
Why is there only one of the two version of this code working?