Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override event prevent default in event listener?

Tags:

jquery

I have a script like this:

$(document).ready(function(){

window.addEventListener('mousewheel', function(e){
    e.preventDefault();
},{passive:false});

});

So now I need to make another function when on click of a button to override the above script and to "return" the default behavior.

I tested this:

$('#button').on('click', function(){
    window.off('mousewheel');
});

And it's not working.

EDIT 2:

Just tested this and still not working:

$('#button').on('click', function(){
    window.removeEventListener('mousewheel', function(e){
        return true;
    }, true);
});
like image 583
Emmanuel-Ab Avatar asked Nov 23 '25 10:11

Emmanuel-Ab


1 Answers

Works Fine!

$(document).ready(function() {

  function foo(e) {
    console.log("mousewheel");
    e.preventDefault();
  }

  window.addEventListener('mousewheel', foo, true);

  $('#button').on('click', function() {
    console.log("click");
    window.removeEventListener('mousewheel', foo, true);

  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="button">Button</button>
like image 164
Tushar Wason Avatar answered Nov 25 '25 00:11

Tushar Wason