English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM addEventListener() method

HTML DOM Document Object

document.addEventListener()Attach event handlers to the document.

Usedocument.removeEventListener()Remove event handlers attached with the addEventListener() method.

Useelement .addEventListener()The method attaches an event handler to the specified element.

Syntax:

document.addEventListener(event, listener, useCapture)
document.addEventListener("click", function() {
alert("Hello World!!!");
});
Test and see‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the addEventListener() method:

Method
addEventListener()1171.09

Parameter value

ParameterDescription
event(Required) The event can be any valid JavaScript event. When using an event, do not use the "on" prefix, for example, use "click" instead of "onclick", "mousedown" instead of "onmousedown".
For a list of all HTML DOM events, please refer to our completeHTML DOM event object reference.
listener(Required) A JavaScript function that can respond to the event that occurs. It can be any JavaScript function that responds to the event.
When an event occurs, the event object is passed as the first parameter to the function. 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 specifying whether the event is executed at the capture phase or the bubble phase. The default is false.
Possible values:
  • true-Event listeners execute at the capture phase

  • false-Event listeners execute at the bubble phase

You can find more information about events in ourin the JavaScript event propagation tutorialRead more about event propagation

Technical details

Return value:None
DOM version:DOM 2Level

More examples

Attach a click event to the document. When the user clicks on any location in the document, output "Hello World" in the <p> element with id="para":

document.addEventListener("click", function() {
document.querySelector("#para").innerHTML = "Hello World!!!";
});
Test and see‹/›

This example references an external "named" function:

document.addEventListener("click", myFunc);
function myFunc() {
   document.body.style.backgroundColor = "coral";
}
Test and see‹/›

You can add many events to the document without overwriting existing events:

document.addEventListener("click", myFunc);
document.addEventListener("click", anotherFunc);
Test and see‹/›

You can add different types of events to the document:

document.addEventListener("mouseenter", myFunc1);
document.addEventListener("click", myFunc2);
document.addEventListener("mouseout", myFunc3);
Test and see‹/›

When passing parameter values, please use an anonymous function that calls the specified function using the parameters:

document.addEventListener("click", function() {
myFunc(x, y);
});
Test and see‹/›

You can also check out

JavaScript Tutorial:Event Listener

JavaScript Tutorial:Event Propagation

HTML DOM Reference:document.removeEventListener()

HTML DOM Reference:element .addEventListener()

HTML DOM Document Object