I needed something similar, but for mousewheel a while back and I made this code:
function createFakeMouseEvent(event, elemento) {
var evt;
if (!(window.mozInnerScreenX == null)) {
// Firefox
evt = document.createEvent("MouseEvents");
evt.initMouseEvent(event, true, true, window, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, null);
elemento.dispatchEvent(evt);
} else {
if ('onwheel' in document) {
// Chrome, PhantomJS, Safari
evt = document.createEvent("MouseEvents");
evt.initMouseEvent(event, 0, 100, window, 0, 0, 0, 0, null, null, null, null);
elemento.dispatchEvent(evt);
} else if ( !! document.createEvent) {
// IE9
evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, false);
elemento.dispatchEvent(evt);
} else if (document.documentMode == 9) {
// IE10+, Safari
var evt = document.createEvent("MouseEvents");
evt.initEvent(event, true, true);
elemento.dispatchEvent(evt);
} else {
// IE8
var evt = document.createEventObject();
elemento.fireEvent(event, evt);
}
}
}
The code has become a bit complex because different browsers have different ways to trigger events... but it should work for whatever.
I recommend taking a look at the documentation of the method
dispatchEvent
, its parameter is an objectEvent
not the event name. An example would be:dispatchEvent(new Event('mouseover'))
;– Wakim
Valew man! I found out and actually I had to create the event, it was like this:
var atual = document.getElementById('teste');

atual.addEventListener('mouseover', function(){ alert('HUE HUE HUE BR') });
atual.dispatchEvent(new Event('mouseover'));
– Douglas dos Santos
Possible duplicate of How to make a Rigger for a SEM jQuery event?
– Woss