1

The following code works. It allows you to press the enter and escape buttons. Here is a jsFiddle

$(document).on('keydown', function(e){
    if (e.which == 13){
        // Enter Key pressed
    }
});
$(document).on('keydown', function(e){
    if (e.which == 27){
        // Esc Key pressed
    }
});

My question is how would I remove the binding for just the enter key?

Jacob Valenta
  • 6,659
  • 8
  • 31
  • 42

1 Answers1

2

e.preventDefault(), and return false; in the function

$(document).on('keydown', function(e){
    if (e.which == 13){
        e.preventDefault();
        return false;
    }
});

or if you mean just the event binding

$(document).off('keydown');

if you mean just for that particular binding you can set a namespace

$(document).on('keydown.mykeydown'...
$(document).off('keydown.mykeydown');
Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
  • The first one seems okay, little long. I may look into the last one, I have never declared a namespace, but it seems to me most like what I want. Is that `.on('keydown.mykeydown')...` the declaration? or only the binding? – Jacob Valenta Jun 30 '13 at 14:38
  • well the first one is just incase you want the enter to be ignored, which is what i thought at first but then saw that you mentioned bind – Patrick Evans Jun 30 '13 at 14:40
  • 1
    And as preventDefault and return false do the same thing in this case, you only need one of them, preferably the first one. – adeneo Jun 30 '13 at 14:40
  • Then the first one is not what I would like. I would just like to remove my logic for what happens when it gets pressed, and give control (default action) back to the web browser – Jacob Valenta Jun 30 '13 at 14:41