Javascript e.keyCode doesn't catch Backspace/Del in IE

I'm trying to catch the pressing event of Backspace and Delete keys using javascript/jQuery with this kind of code.

$("textarea[name=txt]").keypress(function(e){
    var keycode =  e.keyCode ? e.keyCode : e.which;

    if(keycode == 8){ // backspace
        // do somethiing
        alert(keycode);
    }

    if(keycode == 46){ // delete
        // do somethiing
        alert(keycode);
    } 

});

These lines of code works perfectly in Firefox (3.6.12). That means the alert is popped up when Backspace or Delete is pressed. But this is not working in Internet Explorer (8)

Can anyone suggest me a different way to catch these key press events in Internet Explorer?

Answers


If you want to support IE and you use special keys (like delete and backspace) I suggest using keydown/keyup instead.

Special keys

Explorer doesn't fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab.

If you need to detect these keys, do yourself a favour and search for their keyCode onkeydown/up, and ignore both onkeypress and charCode.

You can read more on cross browser issues of Detecting keystrokes (Quirksmode).


Key Code for Backspace will take the value = 83 if we already have a few characters in a Text Box .

The Key Code will be = 8 if there are NO Characters in the Text Box and we are trying to Hit Backspace.


Need Your Help

In-app purchases made via promo codes return empty developer payload string

android google-play in-app-billing promotion-code

I have an app published to the alpha channel, with an in-app (un)managed item that costs $1.

Does your email client let you add custom headers programmatically?

email sendmail email-integration email-spam email-client

I'm working on an anti-spam project (centmail) that involves having the sender use a client plugin that adds a custom header (as well as a signature, but that part's easier). The general problem i...