DOM:element.dispatchEvent
From MDC
Contents |
[edit] Summary
Dispatches an event into the event system. The event is still subject to the same capturing and bubbling behavior as directly dispatched events.
[edit] Syntax
bool = element.dispatchEvent(event)
-
elementis thetargetof the event. -
eventis an event object to be dispatched. - The return value is
false, if at least one of the event handlers which handled this event, called preventDefault. Otherwise it returnstrue.
[edit] Example
This example demonstrates simulating a click on a checkbox using DOM methods. You can view the example in action here.
function simulateClick() {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
var cb = document.getElementById("checkbox");
var canceled = !cb.dispatchEvent(evt);
if(canceled) {
// A handler called preventDefault
alert("canceled");
} else {
// None of the handlers called preventDefault
alert("not canceled");
}
}
[edit] Notes
As demonstrated in the above example, dispatchEvent is the last step of the create-init-dispatch process, which is used for manually dispatching events into the implementation's event model.
The event can be created using document.createEvent method and initialized using initEvent or other, more specific, initialization methods, such as initMouseEvent or initUIEvent.
See also the Event object reference.