HTML | DOM createEvent() Event Method

The createEvent() method in HTML creates an event object of the specified type. The created event must be initialized before use.

Syntax:

document.createEvent( event_type )

event_type: It is the required parameter and used to specify the type of event.

There are many type of events which are listed below:

  • AnimationEvent
  • ClipboardEvent
  • DragEvent
  • FocusEvent
  • HashChangeEvent
  • InputEvent
  • KeyboardEvent
  • MouseEvent
  • PageTransitionEvent
  • PopStateEvent
  • ProgressEvent
  • StorageEvent
  • TouchEvent
  • TransitionEvent
  • UiEvent
  • WheelEvent

Below program illustrates the createEvent() method in HTML:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>
            DOM createEvent() Event Method
        </title>
          
         <style>
            div {
                padding:50px;
                text-align:center;
                background-color: green;
                color: white;
            }
        </style>
    </head>
      
    <body>
        <script>
            document.body.onclick = function() {
                document.getElementById("Geeks").innerHTML 
                            = "Welcome to GeeksforGeeks!";
            };
              
            var onClick = function() {
                var evt = document.createEvent("MouseEvents");
                evt.initMouseEvent("click", true, true, window, 0,
                 0, 0, 0, 0, false, false, false, false, 0, null);
                document.body.dispatchEvent(evt);
            }
              
            onClick();
        </script>
          
        <div id = "Geeks"></div>
      
    </body>
</html>                    

chevron_right


Output:
Before Click on the element:

After Click on the element:

Supported Browsers: The browser supported by DOM createEvent() method are listed below:

  • Google Chrome
  • Apple Safari
  • Firefox
  • Opera
  • Internet Explorer


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.