Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preventDefault does not behave equal in different browsers

I use jQuery preventDefault on a keydown event: http://jsbin.com/ixaqok/edit#javascript,html When running the example code in Firefox and Opera the keypress event still is fired, but in Chrome, IE8 and Safari it's not.

Why? Is preventDefault not supposed to work the same in all browsers?

Thanks!

like image 971
mkurz Avatar asked Feb 23 '23 03:02

mkurz


2 Answers

The problem is that unlike other browsers, Opera only allows you to suppress the default behaviour of keystrokes in the keypress event, not the keydown event. Therefore the solution is to use the keypress event instead.

For more details, use what I consider the definitive page on the mess that is browser key events: http://unixpapa.com/js/key.html. Suppressing default key behaviour is section 2.3.

like image 160
Tim Down Avatar answered Feb 25 '23 16:02

Tim Down


Opera has problem with recognizing and resolving keydown (and some other key releated funcitons)

see:
http://www.quirksmode.org/dom/events/keys.html
http://api.jquery.com/event.preventDefault/ (in discussion)

like image 40
Marek Sebera Avatar answered Feb 25 '23 18:02

Marek Sebera