I have the following JavaScript code, where the removeEventListener
method does not work for me within a class:
class Removerevento {
constructor() {
this._btndos = document.getElementById('btnDos');
this._btntres = document.getElementById('btnTres');
}
btnUno() {
console.log("Boton uno");
this._btndos.addEventListener('click', this.btnDos, false);
this._btntres.addEventListener('click', this.btnTres, false);
}
btnDos() {
console.log('Boton dos');
}
btnTres() {
console.log('Boton tres');
this._btndos.removeEventListener('click', this.btnDos, false);
}
}
let rm = new Removerevento;
document.getElementById('btnUno')
.addEventListener('click', function() {
rm.btnUno();
}, false);
<button id="btnUno">Uno</button>
<button id="btnDos">Dos</button>
<button id="btnTres">Tres</button>
The steps to reproduce the problem are: press button One and then press button 3. Then it says that you can not use removeEventListener
in undefined
.
Why does that happen? How can I solve it?