lundi 27 avril 2020

The `this` keyword inside `addEventListener` is being assigned to window object

I'm writing click event on two buttons which have class mode. The console statement for this keyword is accessing window object rather than the clicked buttons.

let modeButtons = document.querySelectorAll('.mode');
modeButtons.forEach((mode) => {
  mode.addEventListener('click', (event) => {
    console.log(event.target.textContent);
    console.log(this);
  });
});

I don't know why this is happening. When i tried to access the button from event object, it is working but not with the this keyword.




Aucun commentaire:

Enregistrer un commentaire