Pregunta

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?

¿Fue útil?

Solución

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();
    }
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top