evt = document.createEvent("MouseEvents"); evt.initMouseEvent(type, bubbles, cancelable, view, detail, screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, button, document.body.parentNode); var element = document.getElementById('myelement'); element.dispatchEvent(evt);
You may wonder how to specify the coordinates in the initMouseEvent? If I do not know or specify the wrong coordinates of the element to click on, can the event be received and handled? In fact, you do not need to know the coordinates(specifying random values for the coordinate parameters) if you just want to fire the event handler. The event handling function is successfully called passing the event object you created as the parameter(although the coordinates are not real ones). You can even use the initEvent function instead of the initMouseEvent to initialize the event object where you need not specify the coordinates at all.
evt.initEvent( 'click', true, true );