Question

I have some code like this:

$(document).on("keydown", ":not(input)", function(event) {
    if (event.which === 8) {
        event.preventDefault();
    }
});

Everything is working fine except the :not(input) selector. I can't backspace in any input box.

What am I doing wrong?

Was it helpful?

Solution

I can't explain exactly why it's happening this way, it's likely due to the way the event propagates up the dom, however, removing the delegation and using an event.target check seems to work.

http://jsfiddle.net/7KDMn/3/

$(document).on("keydown", function(event) {
    if ( event.which === 8 && !$(event.target).is("input") ) {
        event.preventDefault();
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top