Created
March 28, 2014 01:12
-
-
Save sukima/9822950 to your computer and use it in GitHub Desktop.
This is how I managed to facilitate keyboard events in multiple browsers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <input id="my-input-element" type="text" value="foo"/> | |
var evt, node = document.getElementById('my-input-element'); | |
// Have to use dispatchEvent/fireEvent because jQuery.trigger will not | |
// fire an event attached via addEventListener. Each environment has an | |
// unusual way to trigger a keyup event. | |
if (node.dispatchEvent) { | |
// Sane browsers | |
try { | |
// Chrome, Safari, Firefox | |
evt = new KeyboardEvent('keyup'); | |
} catch (e) { | |
// PhantomJS (wat!) | |
evt = document.createEvent('KeyboardEvent'); | |
evt.initEvent('keyup', true, false); | |
} | |
evt.keyCode = 32; | |
node.dispatchEvent(evt); | |
} else { | |
// IE 8 | |
evt = document.createEventObject('KeyboardEvent'); | |
evt.keyCode = 32; | |
node.fireEvent('onkeyup', evt); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/26979893. This works for me.