1

In Internet Explorer 8, event.which is showing undefined and working fine in FireFox and IE 9. I am using the Textbox 'onkeypress' Event

MarkUp

<asp:TextBox runat="server" ID="tb1" MaxLength="3"
                        onkeypress="return MainCheckStrings(event);" />

It is working fine in FF, Chrome, Safari and Internet Explorer - 9

Any Idea?

Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • Possible duplicate: http://stackoverflow.com/questions/2412419/jquery-event-which-not-working-in-ie – DaveRead Apr 25 '12 at 12:11
  • 1
    SO thread with an explanation of why. hint: searching is your friend. http://stackoverflow.com/questions/3050984/javascript-event-e-which –  Apr 25 '12 at 12:12

3 Answers3

6
var charCode = evt.which || evt.keyCode;
Pankaj
  • 9,749
  • 32
  • 139
  • 283
2

The documentation clearly shows that this property is available only from IE 9 onwards.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • @Guest: Chuck found your link. Read [this answer](http://stackoverflow.com/a/3051784/50079). Specifically, the *"For `keypress` events..."* part. – Jon Apr 25 '12 at 12:14
1

In IE prior 9 the even object is not the same as others so you need to handle that case specifically and same goes for the which property.

function MainCheckStrings(e) {
    if (!e) {
        e = window.event;  // Get event details for IE
        e.which = e.keyCode; // assign which property (so rest of the code works using e.which)
    }
};

Here is a good article on the subject that should explains everything clearly http://www.quirksmode.org/js/keys.html

GillesC
  • 10,647
  • 3
  • 40
  • 55
  • Updated so you can see it. In some IE it doesn't exists but you can assign it and use the rest of the code as expected. – GillesC Apr 25 '12 at 12:14