In my sample code is not working perfectly can you please check and let me know where I’m wrong?
function clickButon (){
var btn = document.getElementById('updateText');
btn.addEventListener('click', function(){
updateHTml();
console.log('event click here ')
// jsFileLoad();
},true)
}
function eventRemove(){
var btn = document.getElementById('updateText');
btn.removeEventListener('click', function(){
console.log('event remove for btn')
}, true)
}
window.onload = function(){
clickButon();
setTimeout(function(){
eventRemove();
}, 2000)
}
2
Answers
To remove the event handler, you have to pass the same function that you passed when adding it.
For example, here you would successfully remove the event handler
Contraty to creating a new function when you’re removing the event listener:
The function you are removing is not the same function you added. The only way to remove a listener is to give it the same function that is already added as a listener. For example:
Note that if you create an identical function, it will still not work, because "identical" is not "same":