You are here: Reference > JavaScript > client-side > event handling > methods > initEvent (event)

initEvent method (event)

Browser support:
9
Initializes an event object created by the createEvent method.
Any type of event can be initialized with this method, but the type-specific initialization methods provide more possibilities for non-basic event types. You can find the possible event types and initialization methods on the page for the createEvent method.
After initialization, the event 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:

object.initEvent (eventName, bubbles, cancelable);
You can find the related objects in the Supported by objects section below.

Parameters:

eventName
Required. String that specifies the name (type) of the event to initialize.
This parameter is case sensitive! Sets the type property of the event object. For a complete list of events, see the page for Events in JavaScript. The initEvent method is appropriate for initialization of basic event types principally, for other events, use the type-specific initialization methods.
Note: custom event names are also allowed.
Events represented by the Event interface:
afterprint
beforecopy
beforecut
beforepaste
beforeprint
beforeunload
blur
bounce
change
CheckboxStateChange
copy
cut
error
finish
focus
hashchange
3.610.65
help
input
load
offline
3
online
3
paste
RadioStateChange
readystatechange
reset
resize
scroll
search
select
selectionchange
selectstart
start
stop
submit
unload
bubbles
Required. Boolean that specifies whether the event can bubble or not. Sets the bubbles property of the event object.
One of the following values:
false
The event cannot bubble up.
true
The event can bubble up.
cancelable
Required. Boolean that specifies whether the event can be canceled or not. Sets the cancelable property of the event object.
One of the following values:
false
The event cannot be canceled.
true
The event can be canceled.

Return value:

This method has no return value.

Example HTML code 1:

This example illustrates the use of the initEvent method to dispatch a change event when the selection of a list control is modified by script:
<head>
    <script type="text/javascript">
        function Select () {
            var fruits = document.getElementById ("fruits");
            if (fruits.selectedIndex == 0) {
                fruits.selectedIndex = 1;
            }
            else {
                fruits.selectedIndex = 0;
            }

                // Dispatch a change event
            if (document.createEvent) {     // all browsers except IE before version 9
                var changeEvent = document.createEvent ("Event");
                changeEvent.initEvent ("change", true, false);
                fruits.dispatchEvent (changeEvent);
            }
            else {
                alert ("Your browser does not support the createEvent method!");
            }
        }
        function OnChange () {
            alert ("The selected fruit is changed!");
        }
    </script>
</head>
<body>
    <select id="fruits" onchange="OnChange ()">
        <option value="Apple">Apple</option>
        <option value="Pear">Pear</option>
        <option value="Peach" selected="selected">Peach</option>
    </select>
    <button onclick="Select ()">Select another fruit!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content