SmartSolution
SmartSolution

Reputation: 2338

How to trigger event on HTML element in IE8?

Below code works fine in Firefox, but not in IE8.

It triggers mouse click event on HTML element (here specified as 'node' ) and invokes function already bound to that element. So its like triggering the click event on given element programmatically.
var oEvent = document.createEvent( \"MouseEvents\" ); oEvent.initMouseEvent(\"click\", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, node); node.dispatchEvent(oEvent);
Can anybody help me to make it work in IE8 ?

Thanks in advance.

Upvotes: 1

Views: 2661

Answers (1)

mplungjan
mplungjan

Reputation: 177691

You only need code like that for Firefox on elements that did not already have a click event!

Just click the element in other browsers - for example node.click()

This means you need to sniff the event. I would try

if (node.click) node.click();
else if (document.createEvent) {
  var oEvent = document.createEvent("MouseEvents");
  if (oEvent.initMouseEvent) { // just to be sure...
    oEvent.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, node);  
    node.dispatchEvent(oEvent); 
  }
}

Upvotes: 2

Related Questions