Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1037782 of Creating and triggering events

  • Revision slug: Web/Guide/Events/Creating_and_triggering_events
  • Revision title: Creating and triggering events
  • Revision id: 1037782
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment

Revision Content

This article demonstrates how to create and dispatch DOM events. Such events are commonly called synthetic events, as opposed to the events fired by the browser itself.

Creating custom events

Events can be created with the Event constructor as follows:

var event = new Event('build');

// Listen for the event.
elem.addEventListener('build', function (e) { ... }, false);

// Dispatch the event.
elem.dispatchEvent(event);

This constructor is supported in most modern browsers (with Internet Explorer being the exception). For a more verbose approach (which works with Internet Explorer), see the old-fashioned way below.

Adding custom data – CustomEvent()

To add more data to the event object, the CustomEvent interface exists and the detail property can be used to pass custom data.
For example, the event could be created as follows:

var event = new CustomEvent('build', { 'detail': elem.dataset.time });

This will then allow you to access the additional data in the event listener:

function eventHandler(e) {
  console.log('The time is: ' + e.detail);
}

The old-fashioned way

The older approach to creating events uses APIs inspired by Java. The following shows an example:

// Create the event.
var event = document.createEvent('Event');

// Define that the event name is 'build'.
event.initEvent('build', true, true);

// Listen for the event.
elem.addEventListener('build', function (e) {
  // e.target matches elem
}, false);

// target can be any Element or other EventTarget.
elem.dispatchEvent(event);

Triggering built-in events

This example demonstrates simulating a click (that is programmatically generating a click event) on a checkbox using DOM methods. View the example in action.

function simulateClick() {
  var event = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': true
  });
  var cb = document.getElementById('checkbox'); 
  var canceled = !cb.dispatchEvent(event);
  if (canceled) {
    // A handler called preventDefault.
    alert("canceled");
  } else {
    // None of the handlers called preventDefault.
    alert("not canceled");
  }
}

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Edge Internet Explorer Opera Safari (WebKit)
Event() constructor 15 11 {{CompatVersionUnknown}} {{CompatNo}} 11.60 6
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} 6

See also

  • {{domxref("document.createEvent()")}}
  • {{domxref("Event.initEvent()")}}
  • {{domxref("EventTarget.dispatchEvent()")}}
  • {{domxref("EventTarget.addEventListener()")}}

Revision Source

<p>This article demonstrates how to create and dispatch DOM events. Such events are commonly called <strong>synthetic events</strong>, as opposed to the events fired by the browser itself.</p>

<h2 id="Creating_custom_events">Creating custom events</h2>

<p>Events can be created with the <a href="/en-US/docs/Web/API/Event"><code>Event</code></a> constructor as follows:</p>

<pre class="brush: js">
var event = new Event('build');

// Listen for the event.
elem.addEventListener('build', function (e) { ... }, false);

// Dispatch the event.
elem.dispatchEvent(event);</pre>

<p>This constructor is supported in most modern browsers (with Internet Explorer being the exception). For a more verbose approach (which works with Internet Explorer), see <a href="#The_old-fashioned_way" title="#The_old-fashioned_way">the old-fashioned way</a> below.</p>

<h3 id="Adding_custom_data_–_CustomEvent()">Adding custom data – CustomEvent()</h3>

<p>To add more data to the event object, the <a href="/en-US/docs/Web/API/CustomEvent">CustomEvent</a> interface exists and the <u><strong>detail</strong></u>&nbsp;property can be used to pass custom data.<br />
 <span style="line-height:1.5">For example, the event could be created as follows:</span></p>

<pre class="brush: js">
var event = new CustomEvent('build', { 'detail': elem.dataset.time });</pre>

<p>This will then allow you to access the additional data in the event listener:</p>

<pre class="brush: js">
function eventHandler(e) {
  console.log('The time is: ' + e.detail);
}
</pre>

<h3 id="The_old-fashioned_way">The old-fashioned way</h3>

<p>The older approach to creating events uses APIs inspired by Java. The following shows an example:</p>

<pre class="brush: js">
// Create the event.
var event = <a href="/en-US/docs/Web/API/Document/createEvent">document.createEvent</a>('Event');

// Define that the event name is 'build'.
event.initEvent('build', true, true);

// Listen for the event.
elem.addEventListener('build', function (e) {
  // e.target matches elem
}, false);

// target can be any Element or other EventTarget.
elem.dispatchEvent(event);

</pre>

<h2 id="Triggering_built-in_events">Triggering built-in events</h2>

<p>This example demonstrates simulating a click (that is programmatically generating a click event) on a checkbox using DOM methods. <a class="external" href="https://developer.mozilla.org/samples/domref/dispatchEvent.html">View the example in action.</a></p>

<pre class="brush: js">
function simulateClick() {
  var event = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': true
  });
  var cb = document.getElementById('checkbox'); 
  var canceled =&nbsp;!cb.dispatchEvent(event);
  if (canceled) {
    // A handler called preventDefault.
    alert("canceled");
  } else {
    // None of the handlers called preventDefault.
    alert("not canceled");
  }
}</pre>

<h2 id="Browser_compatibility" name="Browser_compatibility" style="line-height: 30px; font-size: 2.14285714285714rem;">Browser compatibility</h2>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th style="line-height: 16px;">Feature</th>
   <th style="line-height: 16px;">Chrome</th>
   <th style="line-height: 16px;">Firefox (Gecko)</th>
   <th style="line-height: 16px;">Edge</th>
   <th style="line-height: 16px;">Internet Explorer</th>
   <th style="line-height: 16px;">Opera</th>
   <th style="line-height: 16px;">Safari (WebKit)</th>
  </tr>
  <tr>
   <td><code>Event()</code>&nbsp;constructor</td>
   <td>15</td>
   <td>11</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>11.60</td>
   <td>6</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th style="line-height: 16px;">Feature</th>
   <th style="line-height: 16px;">Android</th>
   <th style="line-height: 16px;">Firefox Mobile (Gecko)</th>
   <th style="line-height: 16px;">IE Mobile</th>
   <th style="line-height: 16px;">Opera Mobile</th>
   <th style="line-height: 16px;">Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>6</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("document.createEvent()")}}</li>
 <li>{{domxref("Event.initEvent()")}}</li>
 <li>{{domxref("EventTarget.dispatchEvent()")}}</li>
 <li>{{domxref("EventTarget.addEventListener()")}}</li>
</ul>
Revert to this revision