views:

289

answers:

2
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?

A: 

Okay problem solved. Apparently all you have to do is remove the "event" parameter from onkeydown(). I think that's because IE doesn't read javascript the way Chrome does, thus it fails to work properly because there isn't a parameter passed along. =.=

Fabian
A: 

But once you remove the event from there, firefox no longer works. Will these not work together?

Bryan