I cannot think from head to head about a good situation when you can use the "on some event" method of the DOM element to handle events on this element.
Best practice is to use addEventListener(or attachEventin older versions of Internet Explorer) as follows:
charfield.addEventListener('keydown', function (e) { alert(e.keyCode); }, false);
If you want to specify also attachEvent:
(function (useListen) {
if (useListen) {
charfield.addEventListener('keydown', alertKeyCode, false);
} else {
charfield.attachEvent('onkeydown', alertKeyCode);
}
})(charfield.addEventListener);
function alertKeyCode(e) {
alert(e.keyCode);
}
source
share