michael
michael

Reputation: 110510

How can I call mouseover event handler programmatically in 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?

Upvotes: 5

Views: 2287

Answers (2)

KooiInc
KooiInc

Reputation: 122888

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');

Upvotes: 4

Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

You can use the fireEvent method available for IE. I am not sure if this will work for FF or other browsers. you can simply fire the event by

buttonObject.fireEvent('onclick');

For more details have a look at MSDN.

Upvotes: 0

Related Questions