keshu_vats
keshu_vats

Reputation: 442

event.which and event.keycode is not working on internet explorer 10 for backspace button

I have to track the alphabet keys and backspace key with JavaScript. I am using below written code to track every key press but unfortunately when i press backspace button in IE i gets nothing any idea regarding the issue.

    $('#crossword').delegate('.grid_input_holder','keypress',function(event){
            alert('keycode : '+event.keycode+' which : '+$(this).val( String.fromCharCode(event.which));
    });

Thanks in advance

Upvotes: 1

Views: 8713

Answers (3)

ANILBABU
ANILBABU

Reputation: 295

The problem araises when you are using IE10 . Because in the IE versions < 9 give

event.which == undefined || event.which == zero 

when special handling key is pressed like Backspace,DEL,INS,HOME,Navigation Keys, Enter, etc. So we could easily identify when a special handling character is pressed and can be handled seperately.

But in IE10 . event.which doesn't give undefined or zero for special handling keys instead it gives keycode.... Surprising thing is , It gives same key code for Dot( . ) and Delete , Single quote( ' ) and Right arrow , etc.

Hence we couldn't identify exactly in IE10 when special handling key is pressed by using neither event.which nor event.keycode .

In such Situations ... Use

event.charCode == 0 

For identifying special handling characters in IE10

Trust me friends, It saved me more than a week's effort.

Upvotes: 2

Amit Aman
Amit Aman

Reputation: 108

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).

OR

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.

Upvotes: 4

keshu_vats
keshu_vats

Reputation: 442

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

Upvotes: 2

Related Questions