
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM addEventListener() Method
The HTML DOM addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.
Target can be any object that supports events, some common targets are Element, or it's children, Document, and Window. Multiple events can be added to one element without overwriting existing event handlers.
The following interactive example demonstrate the usage of the addEventListener() method for different scenarios −
Welcome to Tutorialspoint (click on me after adding the event)
You are at the right place to learn.... Visit for more
Syntax
Following is the syntax of the HTML DOM addEventListener() method −
document.addEventListener(event, listener, useCapture);
Parameters
This method accepts the following parameters −
Parameter | Description |
---|---|
event | It represents type of event being used. Some common events used are click , dblclick, mouseover. |
listener | It can be any valid function that is called upon occurrence of any event. For example:Changing of light theme to dark upon click(event). |
useCapture | This is an optional parameter to control event propagation. It uses boolean value where TRUE represents capturing phase while FALSE represents event bubbling phase. |
Return Value
This method does not return anything.
Example 1: Adding Event to Element
The following example demonstrates the usage of the HTML DOM addEventListener() method. It adds the mouseover event to the div element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM addEventListener()</title> <style> #event { height: 400px; width: 400px; } </style> </head> <body> <p>Mouseover on the div element </p> <div id="event"> This is a div element.</div> <script> document.getElementById("event").addEventListener("mouseover", fun); function fun() { document.getElementById("event").style.backgroundColor = "#04af2f"; } </script> </body> </html>
Example 2: Adding Multiple Events
The following is another example of the HTML DOM addEventListener() method. We use this method to add multiple events, such as click, mouseover, mouseout, etc −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM addEventListener()</title> </head> <body> <p>This is just a random text to select.</p> <button id="click">click me</button> <p id="any"></p> <script> document.addEventListener("mouseover", funtwo); document.getElementById("click").addEventListener("click", fun); document.addEventListener("mouseout", funthree); function funtwo() { document.getElementById("any").innerHTML = "mouseover event"; } function fun() { alert("Welcome to Tutorialspoint"); } function funthree() { document.getElementById("any").innerHTML = "mouseout event"; } </script> </body> </html>
Example 3: Adding Multiple Event Listeners
In the example below, multiple event listeners or functions are added to the mouseover event using the addEventListener() method −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM addEventListener()</title> <style> #event { height: 400px; width: 400px; } </style> </head> <body> <div id="event" class="out"> This is a div element.</div> <script> document.getElementById("event").addEventListener("mouseover", fun); document.getElementById("event").addEventListener("mouseover", funtwo); document.getElementById("event").addEventListener("mouseover", funthree); function fun() { document.getElementById("event").style.backgroundColor = "#04af2f"; } function funtwo() { document.getElementById("event").innerHTML = "This is the second function executing with mouseover"; } function funthree() { alert("This is third function with mouseover"); } </script> </body> </html>
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
addEventListener() | Yes 1 | Yes 12 | Yes 1 | Yes 1 | Yes 7 |