Search code examples
javascriptjqueryeventspreventdefault

restore event to default action in jquery after event.prenventdefault


I used this answer to disable arrow and pgdown and pgup keys actions (scrolling). but I need to set actions of this keys to default (scrolling) after event.preventDefault() called in my jquery code.

How can I do this.


Solution

  • Hard to answer without better specifics, but if I understand what you have properly if you have something you can track state with you can preventDefault when you wish to block this behavior and let it fall through otherwise:

    Psudocode:

    var state = {
       blockArrows: true;
    }
    
    $('#my_widget').keydown(function(e) {
      if(state.blockArrows) {
        e.preventDefault();
      }
      // ...else allow the event to do its normal thing....///
    });