Sometimes, we want to fix preventDefault not working inside passive event listener with JavaScript.
In this article, we’ll look at how to fix preventDefault not working inside passive event listener with JavaScript.
How to fix preventDefault not working inside passive event listener with JavaScript?
To fix preventDefault not working inside passive event listener with JavaScript, we set passive
to false
.
For instance, we write
document.addEventListener(
"wheel",
(e) => {
e.preventDefault();
doStuff(e);
},
{ passive: false }
);
to call addEventListener
to add an event listener for the wheel
event.
We set passive
to false
in the object in the 3rd argument so the e.preventDefault
will work in the event listener.
Conclusion
To fix preventDefault not working inside passive event listener with JavaScript, we set passive
to false
.