Skip to content

Instantly share code, notes, and snippets.

@guyb7
Created November 23, 2017 09:45
Show Gist options
  • Select an option

  • Save guyb7/6d2fccd2c5d8bfe4a32096f6fd263094 to your computer and use it in GitHub Desktop.

Select an option

Save guyb7/6d2fccd2c5d8bfe4a32096f6fd263094 to your computer and use it in GitHub Desktop.
Trigger click, mousedown or other MouseEvents in JavaScript
/*
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