preventDefault() on keyup event not working preventDefault() on keyup event not working google-chrome google-chrome

preventDefault() on keyup event not working


Listening to keyup event is too late for calling preventDefault, try listening to keypress or keydown instead.

$('input[type=text], textarea').on('keydown', function(event){    if (event.which == 192) {        console.log('192');        event.preventDefault();    }});

Note that jQuery normalizes which property and it's cross-browser.

http://jsfiddle.net/763ys/


There is no default behaviour for .keyup(); it fires after the key press which triggers the default behaviour. Use .keydown() or.keypress()` instead.

source: http://api.jquery.com/category/events/keyboard-events/


You are using JQuery, which normalises the differences between browsers, so you only need to to check the evt.which property. See the JQuery documentation: http://api.jquery.com/keyup/

As others have said - keyup is too late to stop the processing of the key. Use keydown or keypress instead. http://api.jquery.com/category/events/keyboard-events/