I need to generate keyup events in IE 8 using native DOM functions (no jQuery). The following code generates, fires, and receives the event, but the keyCode is always 0. How do I properly pass the keyCode?
<form><input id="me" type="submit" /></form>
<script type="text/javascript">
var me = document.getElementById("me");
me.attachEvent("onkeyup", function(e) {
alert(e.keyCode); // => 0
});
document.getElementById("me").fireEvent('onkeyup', 13);
</script>
The keyup event is fired when a key is released. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .
The onkeyup event occurs when the user releases a key (on the keyboard). Tip: The order of events related to the onkeyup event: onkeydown. onkeypress.
JavaScript tab key code is 9. Use the keydown event to get the tab key code or char code in JavaScript.
Figured it out. The solution is to create an event object, assign the keycode, and fire it from the node.
var e = document.createEventObject("KeyboardEvent");
e.keyCode = keyCode;
node.fireEvent("onkeyup", e);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With