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 is used to specify the type of event. There are many types of events which are listed below:

 
 

Example: Below program illustrates the createEvent() method in HTML.

HTML




<!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("Beginner").innerHTML
                = "Welcome to w3wiki!";
        };
        let onClick = function () {
            let 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="Beginner"></div>
</body>
</html>


Output: 

 

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

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 9
  • Firefox 1
  • Opera 7
  • Safari 1


Contact Us