Created
November 23, 2017 09:45
-
-
Save guyb7/6d2fccd2c5d8bfe4a32096f6fd263094 to your computer and use it in GitHub Desktop.
Trigger click, mousedown or other MouseEvents in JavaScript
This file contains hidden or 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
| /* | |
| Common MouseEvents: | |
| mousedown | |
| mouseenter | |
| mouseleave | |
| mousemove | |
| mouseout | |
| mouseover | |
| mouseup | |
| */ | |
| module.exports = ({ event, el, selector }) => { | |
| el = typeof el !== 'undefined' ? el : document.querySelectorAll(selector)[0]; | |
| const clickEvent = document.createEvent ('MouseEvents'); | |
| clickEvent.initEvent(event, true, true); | |
| el.dispatchEvent(clickEvent); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment