HTML DOM addEventListener() Method
- Previous page accessKey
- Next page appendChild()
- Go back to the previous level HTML DOM Elements Object
Definition and Usage
addEventListener()
The method attaches an event handler to an element.
Example
Example 1
Add a click event to the <button> element:
element.addEventListener("click", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "Hello World"; }
Example 2
More compact code:
element.addEventListener("click", function() { document.getElementById("demo").innerHTML = "Hello World"; });
More examples can be found at the bottom of the page.
Syntax
element.addEventListener(type, function, useCapture)
Parameters
Parameters | Description |
---|---|
type |
Required. The name of the event. Do not use the "on" prefix. Use "click" instead of "onclick". |
function | Required. The function to be executed when the event occurs. |
useCapture |
Optional (default = false).
|
Return Value
None.
Technical Details
This method adds the specified event listener function to the current node's listener collection to handle the specified type type the event. useCapture is set to true, then the listener is registered as a capture event listener. If useCapture is set to false, it is registered as a normal event listener.
addEventListener() may be called multiple times, registering multiple event handlers for the same type of event on the same node. However, the DOM cannot determine the order in which multiple event handlers are called.
If an event listener function is registered with the same type and useCapture If the same parameter is registered twice, the second registration will be ignored. If a new event listener is registered on the same node while processing 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, event listeners registered on the original node will not be copied.
This method is also Document and Window defined on the object, and work in a similar manner.
More examples
Example 3
You can add many events to the same element:
element.addEventListener("click", myFunction1); element.addEventListener("click", myFunction2);
Example 4
You can add different events to the same element:
element.addEventListener("mouseover", myFunction); element.addEventListener("click", someOtherFunction); element.addEventListener("mouseout", someOtherFunction);
Example 5
If you need to pass parameter values, please use 'anonymous functions':
element.addEventListener("click", function() { myFunction(p1, p2); });
Example 6
Change the background color of the <button> element:
element.addEventListener("click", function() { this.style.backgroundColor = "red"; });
Example 7
The difference between bubbling and capturing:
element1.addEventListener("click", myFunction, false); element2.addEventListener("click", myFunction, true);
Example 8
Delete event handler:
element.addEventListener("mousemove", myFunction); element.removeEventListener("mousemove", myFunction);
Browser support
element.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 accessKey
- Next page appendChild()
- Go back to the previous level HTML DOM Elements Object