Babiker
Babiker

Reputation: 18798

event.which in IE

Why doesn't event.which work in IE?

This my code, which works fine in Firefox and Chrome but not in IE.

$("#inputFeild").keypress(function(event){
    alert(event.which);
});

#inputFeild is a textarea.

Upvotes: 0

Views: 1135

Answers (1)

Nick Craver
Nick Craver

Reputation: 630419

The keypress event in particular is unreliable for the keycode, use the appropriate event for whatever you're doing...for example if you need the value, use keyup instead:

$("#inputFeild").keyup(function(event){
  alert(event.which);
});

The .keypress() documentation notes a few of these differences:

Note that keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A" is reported as 65 by all events. Because of this distinction, when catching special keystrokes such as arrow keys, .keydown() or .keyup() is a better choice.

Upvotes: 3

Related Questions