jquerytriggersjquery-onjquery-trigger

jQuery stacking and unstacking triggers


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?


Solution

  • 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');