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?
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.
If you want to support IE and you use special keys (like
delete
andbackspace
) I suggest usingkeydown
/keyup
instead.You can read more on cross browser issues of Detecting keystrokes (Quirksmode).