user5804438
user5804438

Reputation:

Opposite of e.preventDefault and e.stopPropagation();

I wrote some javascript code that prevents scrolling on focus. How do I disable it after focus is lost?

Focus

e.preventDefault();
e.stopPropagation();

Focus out

e.preventDefault = false;
e.stopPropagatio = false;

Upvotes: 3

Views: 21213

Answers (2)

user5804438
user5804438

Reputation:

Fixed by unbinding the event.

Focus

    $('.form-control').focus(function() {  
            $('#element').on('scroll touchmove mousewheel', function(e){
                e.preventDefault();
                e.stopPropagation();
        })
});

Focus out

$('.form-control').focusout(function() {
        $('#element').unbind();
});

Upvotes: 4

Oriol
Oriol

Reputation: 288670

You can't undo preventDefault nor stopPropagation.

Just stop calling them, e.g.

var stopScroll = true;
element.addEventListener('scroll', function() {
  if (stopScroll) {
    e.preventDefault();
    e.stopPropagation();
  }
});
// ...
stopScroll = false;
function listener() {
  e.preventDefault();
  e.stopPropagation();
}
element.addEventListener('scroll', listener);
// ...
element.removeEventListener('scroll', listener);

Upvotes: 1

Related Questions