Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call mouseover event handler programmatically in JavaScript

Tags:

javascript

In my html, I have an html element with a mouseover event handler. Can you please tell me if it is possible for me to invoke that event handler programmically in JavaScript?

like image 681
michael Avatar asked Oct 15 '22 04:10

michael


1 Answers

It is possible. Here's a cross browser function to fire an event:

function eventFire(el, etype){
    if (el.fireEvent) {
      el.fireEvent('on' + etype);
    } else {
      var evObj = document.createEvent('Events');
      evObj.initEvent(etype, true, false);
      el.dispatchEvent(evObj);
    }
}
// => exmaples
// => eventFire(myDiv,'mouseover');
// => eventFire(myButton,'click');
like image 75
KooiInc Avatar answered Oct 20 '22 15:10

KooiInc