In IE, I can just call element.click()
from JavaScript – how do I accomplish the same task in Firefox? Ideally I’d like to have some JavaScript that would work equally well cross-browser, but if necessary I’ll have different per-browser JavaScript for this.
Advertisement
Answer
The document.createEvent
documentation says that “The createEvent method is deprecated. Use event constructors instead.“
So you should use this method instead:
JavaScript
x
6
1
var clickEvent = new MouseEvent("click", {
2
"view": window,
3
"bubbles": true,
4
"cancelable": false
5
});
6
and fire it on an element like this:
JavaScript
1
2
1
element.dispatchEvent(clickEvent);
2
as shown here.