JavaScript HTML DOM EventListener
The addEventListener() Method
Example
Trigger a listener event when the user clicks the button:
document.getElementById("myBtn").addEventListener("click", displayDate);
Try it »
The addEventListener() method is used to add an event handler to a specified element.
Event handlers added by the addEventListener() method do not overwrite existing event handlers.
You can add multiple event handlers to an element.
You can add multiple event handlers of the same type to the same element, such as two "click" events.
You can add event listeners to any DOM object, not just HTML elements, such as the window object.
The addEventListener() method allows simpler control of events (bubbling and capturing).
When you use the addEventListener() method, JavaScript is separated from the HTML markup, making it more readable; you can also add event listeners without controlling the HTML markup.
You can use the removeEventListener() method to remove event listeners.
Syntax
element
.addEventListener(
event, function, useCapture
);
The first parameter is the type of event (such as "click" or "mousedown").
The second parameter is the function called after the event is triggered.
The third parameter is a Boolean value describing whether the event is bubbling or capturing. This parameter is optional.
![]() |
Note: Do not use the "on" prefix. For example, use "click" instead of "onclick". |
|---|
Adding an Event Handler to an Element
Example
Alert "Hello World!" when the user clicks the element:
element
.addEventListener("click", function(){ alert("Hello World!"); });
Try it »
You can use a function name to reference an external function:
Example
Alert "Hello World!" when the user clicks the element:
element
.addEventListener("click", myFunction);
function myFunction() {
alert ("Hello World!");
}
Try it »
Adding Multiple Event Handlers to the Same Element
The addEventListener() method allows adding multiple events to the same element without overwriting existing events:
Example
element
.addEventListener("click", myFunction);
element
.addEventListener("click", mySecondFunction);
Try it »
You can add different types of events to the same element:
Example
element
.addEventListener("mouseover", myFunction);
element
.addEventListener("click", mySecondFunction);
element
.addEventListener("mouseout", myThirdFunction);
Try it »
Adding an Event Handler to the Window Object
The addEventListener() method allows you to add event listeners to HTML DOM objects, such as HTML elements, the HTML document, the window object, or other supported event objects such as the xmlHttpRequest object.
Example
Add an event listener when the user resizes the window:
window.addEventListener("resize", function(){
document.getElementById("demo").innerHTML =
sometext
;
});
Try it »
Passing Arguments
When passing parameter values, use an "anonymous function" to call a function with parameters:
Example
element
.addEventListener("click", function(){ myFunction(p1, p2); });
Try it »
Event Bubbling or Event Capturing?
There are two ways of event propagation: bubbling and capturing.
Event propagation defines the order in which element events are triggered. If you insert the
element into the
element, which element's "click" event is triggered first?
In * bubbling *, the event of the inner element is triggered first, then the outer element, that is:
element's click event is triggered first, then the
In * capturing *, the event of the outer element is triggered first, then the inner element's event, that is:
element's click event.
The addEventListener() method can specify the "useCapture" parameter to set the propagation type:
addEventListener(
event
,
function
,
useCapture
);
The default value is false, meaning bubbling; when the value is true, the event uses capturing.
Example
document.getElementById("myDiv").addEventListener("click", myFunction, true);
Try it »
The removeEventListener() Method
The removeEventListener() method removes event handlers added by the addEventListener() method:
Example
element
.removeEventListener("mousemove", myFunction);
Try it »
Browser support
The numbers in the table indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| addEventListener() | 1.0 | 9.0 | 1.0 | 1.0 | 7.0 |
| removeEventListener() | 1.0 | 9.0 | 1.0 | 1.0 | 7.0 |
Note: IE 8 and earlier IE versions, and Opera 7.0 and earlier, do not support the addEventListener() and removeEventListener() methods. However, for these browser versions you can use the detachEvent() method to remove event handlers:
element.attachEvent(event, function);
element.detachEvent(event, function);
Example
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
x.attachEvent("onclick", myFunction);
}
Try it »
HTML DOM Event Object Reference Manual
For all HTML DOM events, see our completeHTML DOM Event Object Reference Manual。
