EventTarget Interface

Understanding the EventTarget interface can make coding much easier to comprehend when working with events triggered by user actions. There are various types of events, and knowing how to handle them is essential for understanding JavaScript and its use of this interface. For example, the addEventListener()method adds a function that responds to certain events (e.g., mousedown).

EventTarget

The EventTarget interface is implemented by objects that can receive events and may have listeners for them. In other words, any target of events implements the three methods associated with this interface.

Element, and its children, as well as Document and Window, are the most common event targets, but other objects can be event targets, too. For example XMLHttpRequest, AudioNode, and AudioContext are also event targets.

Many event targets (including elements, documents, and windows) also support setting event handlers via onevent properties and attributes.


Constructor

EventTarget()

Creates a new EventTarget object instance.


Instance methods

EventTarget.addEventListener()

Registers an event handler of a specific event type on the EventTarget.

EventTarget.removeEventListener()

Removes an event listener from the EventTarget.

EventTarget.dispatchEvent()

Dispatches an event to this EventTarget.


Source: Mozilla, https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
Creative Commons License This work is licensed under a Creative Commons Attribution 2.5 License.

Last modified: Friday, July 21, 2023, 11:12 AM