In order to use some more "equivalent" to jQuery's on
method, you shouldn't use the onkeydown
and onkeyup
handlers. Use addEventListener
or attachEvent
. attachEvent
is specifically for older versions of IE, so addEventListener
is the standard and is used by all other browsers. But you should always include support, so you can make a function to handle it all. Try:
function addEvent(element, eventName, callback) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
} else if (element.attachEvent) {
element.attachEvent("on" + eventName, callback);
}
}
addEvent(window, "keydown", Keypress);
addEvent(window, "keyup", Keyoff);
This allows you to add multiple handlers, just like jQuery's on
method does. Setting the .onkeydown
and .onkeyup
properties allows only one handler (unless you want to overwrite another). There's a lot more that the addEvent
function could do, to make a standard, cross-browser event handling (an example is what happens based on the return type of the callback). It's really not important for now - if you want complete cross browser compatibility, that's what jQuery's for :)
References: