HTML DOM addEventListener()
Method
Example
Add a click event to the document. When the user clicks anywhere in the document, output "Hello World" on the <p>
element with id="demo":
document.addEventListener("click", function(){
document.getElementById("demo").innerHTML = "Hello World";
});
Definition and Usage
The document.addEventListener()
method is used to attach an event handler to the document.
Tip: You can use the document.removeEventListener() method to remove an event handler that has been attached with the addEventListener()
method.
Tip: Use the element.addEventListener() method to attach an event handler to a specified element.
Browser Support
The numbers in the table specify the first browser version that fully supports the method.
Method | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
addEventListener() | 1.0 | 9.0 | 1.0 | 1.0 | 7.0 |
Note: The addEventListener()
method is not supported by Internet Explorer 8 and earlier versions, and Opera 7.0 and earlier. However, for these browser versions, you can use the attachEvent()
method to attach event handlers (for cross-browser compatibility, see "More Examples").
Syntax
Parameter Values
Parameter | Description |
---|---|
event | Required. A string that specifies the event name. <br> <br> Note: Do not use the "on" prefix. For example, use "click" instead of "onclick". <br> <br> Tip: For a complete list of all HTML DOM events, see our HTML DOM Event Object Reference. |
function | Required. Specifies the function to run when the event occurs. <br> <br> When the event occurs, an event object is passed to the function as the first parameter. The type of the event object depends on the specified event. For example, the "click" event belongs to the MouseEvent object. |
useCapture | Optional. A Boolean value that specifies whether the event should be executed in the capturing or bubbling phase. <br> <br> Possible values: true - The event handler is executed in the capturing phase. <br> false - Default. The event handler is executed in the bubbling phase. |
Technical Details
DOM Version: | DOM Level 2 Events |
---|---|
Return Value: | No return value |
--- | --- |
Modification Record: | The useCapture parameter is optional in Firefox 6 and Opera 11.60 (it has always been optional in Chrome, IE, and Safari). |
--- | --- |
More Examples
Example
You can refer to an external function by name:
document.addEventListener("click", myFunction);
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
Example
You can add many events to the document, and the events do not overwrite existing events.
This example demonstrates how to add two click events to the document:
document.addEventListener("click", myFunction);
document.addEventListener("click", someOtherFunction);
Example
You can add different types of events to the document.
This example demonstrates how to add multiple events to the document:
document.addEventListener("mouseover", myFunction);
document.addEventListener("click", someOtherFunction);
document.addEventListener("mouseout", someOtherFunction);
Example
When passing parameter values, use an "anonymous function" to call a function with parameters:
document.addEventListener("click", function() {
myFunction(p1, p2);
});
Example
Change the background color of the <body>
element:
document.addEventListener("click", function(){
document.body.style.backgroundColor = "red";
});
Example
Use the removeEventListener()
method to remove an event handler that was added with the addEventListener()
method:
// Add an event handler to the document
document.addEventListener("mousemove", myFunction);
// Remove the event handler from the document
document.removeEventListener("mousemove", myFunction);
Example
If the browser does not support the addEventListener()
method, you can use the attachEvent()
method instead.
This example demonstrates a cross-browser solution:
if (document.addEventListener) { // All major browsers, except IE 8 and earlier
document.addEventListener("click", myFunction);
} else if (document.attachEvent) { // IE 8 and earlier
document.attachEvent("onclick", myFunction);
}
Related Pages
JavaScript Tutorial: HTML DOM EventListener
JavaScript Reference: element.addEventListener()