daily pastebin goal
7%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener('keydown', onKeyDown, false);
  2. document.querySelector('#test').addEventListener('click', onButtonClick, false);
  3.  
  4.  
  5. function onKeyDown(e){
  6.     console.log("onKeyDown...");
  7. }
  8.  
  9. function onButtonClick(){
  10.     trigger(window, 'click');
  11. }
  12.  
  13.  
  14. function trigger(el, eventName, data){
  15.     data = typeof data === 'undefined' ? {} : data;
  16.  
  17.     // Native
  18.     if (window.CustomEvent) {
  19.         const event = new CustomEvent(eventName, {detail: data});
  20.     }
  21.     else{
  22.         const event = document.createEvent('CustomEvent');
  23.         event.initCustomEvent(eventName, true, true, data);
  24.     }
  25.  
  26.     el.dispatchEvent(event);
  27. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top