HTML DOM addEventListener()
Method
Example
Add a click event to a <button>
element. When the user clicks the button, output "Hello World" on the <p>
element with id="demo":
document.getElementById("myBtn").addEventListener("click", function(){
document.getElementById("demo").innerHTML = "Hello World";
});
Definition and Usage
The addEventListener()
method is used to attach an event handler to a specified element.
Tip: Use the removeEventListener() method to remove an event handler that has been attached with the addEventListener()
method.
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: Internet Explorer 8 and earlier versions do not support the addEventListener()
method, and Opera 7.0 and earlier versions do not support it either. However, for browsers that do not support this function, you can use the attachEvent() method to add event handlers (see "More Examples" for a cross-browser solution).
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> The event object is passed to the function as the first parameter. The type of the event object depends on the specific 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: <br> 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 |
Record: | In Firefox 6 and Opera 11.60, the useCapture parameter is optional. (It has always been optional in Chrome, IE, and Safari). |
More Examples
Example
You can refer to an external function by name.
This example demonstrates how to execute a function when the user clicks a <button>
element:
document.getElementById("myBtn").addEventListener("click", myFunction);
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
Example
You can add many events to the same element without overwriting existing events.
This example demonstrates how to add two click events to a <button>
element:
document.getElementById("myBtn").addEventListener("click", myFunction);
document.getElementById("myBtn").addEventListener("click", someOtherFunction);
Example
You can add different types of events to the same element.
This example demonstrates how to add multiple events to the same <button>
element:
document.getElementById("myBtn").addEventListener("mouseover", myFunction);
document.getElementById("myBtn").addEventListener("click", someOtherFunction);
document.getElementById("myBtn").addEventListener("mouseout", someOtherFunction);
Example
When passing parameter values, use an "anonymous function" to call a function with parameters:
document.getElementById("myBtn").addEventListener("click", function() {
myFunction(p1, p2);
});
Example
Change the background color of a <button>
element:
document.getElementById("myBtn").addEventListener("click", function(){
this.style.backgroundColor = "red";
});
Example
Use the optional parameter useCapture to demonstrate the difference between the bubbling and capturing phases:
document.getElementById("myDiv").addEventListener("click", myFunction, true);
Example
Use the removeEventListener()
method to remove an event handler added with the addEventListener()
method:
// Add event handler to <div>
document.getElementById("myDIV").addEventListener("mousemove", myFunction);
// Remove event handler from <div>
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);
Example
If the browser does not support the addEventListener()
method, you can use the attachEvent()
method as an alternative.
This example demonstrates a cross-browser solution:
var x = document.getElementById("myBtn");
if (x.addEventListener) {
// All major browsers, except IE 8 and earlier
x.addEventListener("click", myFunction);
} else if (x.attachEvent) {
// IE 8 and earlier versions
x.attachEvent("onclick", myFunction);
}
Related Pages
JavaScript Tutorial: HTML DOM EventListener
JavaScript Reference: document.addEventListener()