HTML DOM Document addEventListener() Method
- Previous page activeElement
- Next page adoptNode()
- Go back to the previous level HTML DOM Documents
Definition and Usage
addEventListener()
The method attaches an event handler to the document.
Instance
Example 1
Add a click event to the document:
document.addEventListener("click", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "Hello World"; }
Simpler syntax:
document.addEventListener("click", function(){ document.getElementById("demo").innerHTML = "Hello World"; });
Example 2
You can add multiple event listeners to the document:
document.addEventListener("click", myFunction1); document.addEventListener("click", myFunction2);
Example 3
You can add different types of events:
document.addEventListener("mouseover", myFunction); document.addEventListener("click", someOtherFunction); document.addEventListener("mouseout", someOtherFunction);
Example 4
When passing parameters, use 'anonymous function' to call a function with parameters:
document.addEventListener("click", function() { myFunction(p1, p2); });
Example 5
Change the background color of the document:
document.addEventListener("click", function(){ document.body.style.backgroundColor = "red"; });
Example 6
Use the removeEventListener() method:
// Add event listener document.addEventListener("mousemove", myFunction); // Remove event listener document.removeEventListener("mousemove", myFunction);
Syntax
document.addEventListener(type, function, capture)
Parameters
Parameters | Description |
---|---|
type |
Mandatory. Event name. Do not use the 'on' prefix. Use 'click' instead of 'onclick'. All HTML DOM events are listed at: |
function |
Mandatory. The function that runs when an event occurs. 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. |
capture |
Optional (default = false).
|
Return value
None.
Technical details
This method adds the specified event listener function to the listener collection of the current node to handle the specified type of event. If capture is set to true, the listener is registered as a capture event listener. If capture is set to false, it is registered as a normal event listener.
addEventListener()
it may be called multiple times, multiple event handlers can be registered for the same type of event on the same node. However, it should be noted that DOM cannot determine the order in which multiple event handlers are called.
If an event listener function is registered on the same node with the same type and capture the parameter is registered twice, the second registration will be ignored. If a new event listener is registered on a node while handling an event on that node, the new event listener will not be called for that event.
When Node.cloneNode()
Method or Document.importNode()
When copying a Document node, this method does not copy the event listeners registered for the original node.
This method is also defined on the Document and Window objects, and operates in a similar manner.
Browser support
document.addEventListener
It is a DOM Level 2 (2001) feature.
All browsers fully support it:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9-11 | Support | Support | Support | Support |
Related pages
Element methods
Document methods
Tutorial
- Previous page activeElement
- Next page adoptNode()
- Go back to the previous level HTML DOM Documents