The createEvent()
method allows you to simulate any event.
In this example, the red div will get a new star every time you mouse over it.
<!DOCTYPE html> <html> <body> <style> div {//from ww w . j a v a2 s. com padding:50px; background-color: Tomato; color: white; } </style> <script> function myFunction(event) { var x = document.createEvent("MouseEvent"); x.initMouseEvent("mouseover", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); document.getElementById("myDiv").dispatchEvent(x); } </script> <div onmouseover="this.innerHTML += '*';" id="myDiv">*</div> <br> <button onclick="myFunction(event)">Simulate Mouse Over</button> </body> </html>
The createEvent()
method creates an event object.
Parameter Values
Parameter | Description |
---|---|
type | Required. A String that specifies the type of the event. |
Possible values:
AnimationEvent// w ww . j a v a 2 s . c om ClipboardEvent DragEvent FocusEvent HashChangeEvent InputEvent KeyboardEvent MouseEvent PageTransitionEvent PopStateEvent ProgressEvent StorageEvent TouchEvent TransitionEvent UiEvent WheelEvent
The createEvent()
method returns An Event object.