You are here: Reference > JavaScript > client-side > event handling > methods > createEvent (document, XMLDocument)
createEvent method (document, XMLDocument)
9 | ||||
Creates an event object with the specified type.
After a new event object is created, initialize it first (see the eventType parameter).
When the event is initialized, it can be dispatched with the dispatchEvent method.
In Internet Explorer before version 9, use the createEventObject method to create a new event object and the fireEvent method to dispatch it.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. String that specifies the type of the event to be created.
One of the following values:
|
Return value:
Returns the newly created event object.
Example HTML code 1:
This example illustrates the use of the createEvent and createEventObject methods:
|
||||
<head> <script type="text/javascript"> function InitMouseDown (event) { if (event.initMouseEvent) { // all browsers except IE before version 9 var mousedownEvent = document.createEvent ("MouseEvent"); mousedownEvent.initMouseEvent ("mousedown", true, true, window, 0, event.screenX, event.screenY, event.clientX, event.clientY, event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, 0, null); event.target.dispatchEvent (mousedownEvent); } else { if (document.createEventObject) { // IE before version 9 var mousedownEvent = document.createEventObject (window.event); mousedownEvent.button = 1; // left button is down event.srcElement.fireEvent ("onmousedown", mousedownEvent); } } } </script> </head> <body> <button onmouseover="InitMouseDown (event);" onmousedown="alert ('mousedown event occurred')"> Hover over this button! </button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the difference between the dispatchEvent and fireEvent methods. The dispatchEvent method invokes the default action of the event, the fireEvent method does not:
|
||||
<head> <script type="text/javascript"> function InitClick (event) { if (event.initMouseEvent) { // all browsers except IE before version 9 var clickEvent = document.createEvent ("MouseEvent"); clickEvent.initMouseEvent ("click", true, true, window, 0, event.screenX, event.screenY, event.clientX, event.clientY, event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, 0, null); event.target.dispatchEvent (clickEvent); } else { if (document.createEventObject) { // IE before version 9 var clickEvent = document.createEventObject (window.event); clickEvent.button = 1; // left click event.srcElement.fireEvent ("onclick", clickEvent); } } } </script> </head> <body> <input type="checkbox" onmouseover="InitClick (event);" onclick="alert ('click event occurred')"/>Hover over the check box! </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
Related pages:
event
initEvent
initDragEvent
initKeyEvent
initMessageEvent
initMouseEvent
initMutationEvent
initOverflowEvent
initTextEvent
initUIEvent
dispatchEvent
createEventObject
initEvent
initDragEvent
initKeyEvent
initMessageEvent
initMouseEvent
initMutationEvent
initOverflowEvent
initTextEvent
initUIEvent
dispatchEvent
createEventObject
External links:
User Contributed Comments