64
var clickEvent = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
var element = document.getElementById('element-id');
var cancelled = !element.dispatchEvent(clickEvent);
if (cancelled) {
// A handler called preventDefault.
alert("cancelled");
} else {
// None of the handlers called preventDefault.
alert("not cancelled");
}
element.dispatchEvent is supported in all major browsers. The example above is based on an sample simulateClick() function on MDN.
This is a perfect example of where you should use a javascript library like Prototype or JQuery to abstract away the cross-browser differences.
I haven't used jQuery, but IIRC, the first method mentioned doesn't trigger the onclick handler.
I'd call the associated onclick method directly, if you're not using the event details.