Why doesn't onkeydown working properly on IE?

Posted by Fabian on Stack Overflow See other posts from Stack Overflow or by Fabian
Published on 2010-03-26T12:51:10Z Indexed on 2010/03/26 12:53 UTC
Read the original article Hit count: 484

Filed under:
|
|
|
|
function checkEnter(event) {
    var charcode;
    if (event && event.which) {
        charcode = event.which;
        alert("Case 1. event.which is " + charcode);
    }

    else if (event && !event.which) {
        charcode = event.keyCode;
        alert("Case 2. event.keyCode is " + charcode);
    }

    document.getElementById("text1").value="";
}

<input type="text" id="text1" onkeyup="checkEnter(event)" />

The above function works on both IE7 and Chrome.

function checkKeyPressed() {
    document.onkeydown = function(event) {
        var charcode;
        if (event && event.which) {
            charcode = event.which;
            alert("charcode is " + charcode);
        }

        else if (event && !event.which) {
            charcode = event.keyCode;
            alert("charcode (keyCode) is " + charcode);
        }
    }
}

<input type="button" id="button1" onclick="checkKeyPressed(event)" value="Button" />

However this one works only in Chrome. Any idea why?

© Stack Overflow or respective owner

Related posts about onkeydown

Related posts about chrome