I need to generate keyup events in IE 8 using native DOM functions (without jQuery). The following code generates, fires, and receives an event, but keyCode is always 0. How do I pass keyCode correctly?
<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>
Figured it out. The solution is to create an event object, assign a key code, and start it from node.
var e = document.createEventObject("KeyboardEvent"); e.keyCode = keyCode; node.fireEvent("onkeyup", e);
e = e || window.event; keycode = e.keyCode || e.which;
This will make events work in all browsers. Also, I prefer to use me.onkeyup = function(e) { ... } , but it's "just a personal preference (I know the flaws of this method, but I have smart ways to get around them)
me.onkeyup = function(e) { ... }
Source: https://habr.com/ru/post/897847/More articles:In PHP, how do I make a mySQL select query that contains both quotation marks and apostrophes? - phpC # Loading a website into a string using C # WebClient or HttpWebRequest - c #WebClient prohibits opening a page on Wikipedia? - httpWordpress Taxonomy - How to find out which object_id? - databaseCreating a screenshot of the gtk.Window file - pythonHow to get a PNG (alpha channel) screenshot of a Gtk window? - monoScreenshot of a specific c / GTK window - csave current window as image using gtk # - gtkFile Identification in Plone BlobStorage - ploneJoda - remove seconds, milliseconds from PeriodFormat.getDefault (). Print ()? - javaAll Articles