The EventListener
interface represents an object that can handle an event dispatched by an EventTarget
object.
Note: Due to the need for compatibility with legacy content, EventListener
accepts both a function and an object with a handleEvent()
property function. This is shown in the example below.
Properties
This interface neither implements, nor inherits, any properties.
Methods
This interface doesn't inherit any methods.
EventListener.handleEvent()
- A function that is called whenever an event of the specified type occurs.
Example
HTML
<button id="btn">Click here!</button>
JavaScript
const buttonElement = document.getElementById('btn');
// Add a handler for the 'click' event by providing a callback function.
// Whenever the element is clicked, a pop-up with "Element clicked!" will
// appear.
buttonElement.addEventListener('click', function (event) {
alert('Element clicked through function!');
});
// For compatibility, a non-function object with a `handleEvent` property is
// treated just the same as a function itself.
buttonElement.addEventListener('click', {
handleEvent: function (event) {
alert('Element clicked through handleEvent property!');
}
});
Result
See Also:
Specifications
Specification | Status | Comment |
---|---|---|
DOMThe definition of 'EventListener' in that specification. | Living Standard | No change. |
Document Object Model (DOM) Level 2 Events SpecificationThe definition of 'EventListener' in that specification. | Obsolete | Initial definition. |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
EventListener
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1 |
IE
Full support 9 |
Opera
Full support 7 |
Safari
Full support 1 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
handleEvent
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1 |
IE
Full support 9 |
Opera
Full support 7 |
Safari
Full support 1 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
EventListener by Mozilla Contributors is licensed under CC-BY-SA 2.5.