
- 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 removeEventListener() Method
The HTML DOM removeEventListener() method is used to remove an EventListener that was previously added to an element.
In JavaScript, an EventListener is a procedure or a function that is executed when a specified event occurs, such as "click", "mouseover", or "mouseout".
Syntax
Following is the syntax of the HTML DOM removeEventListener() method −
element.removeEventListener(event, function, capture);
Parameters
This method accepts three parameters as mentioned below −
Parameter | Description |
---|---|
event | the type of the event you want to remove. |
function | function that handles the event. |
capture | It is optional decides the removal of an EventListener: It sets 'true' for capturing phase and 'false' for bubbling phase(default). |
Return Value
This method does not return any value.
Example 1: Remove Event Listener From Button
The following is a basic example of the HTML DOM removeEventListener() method. It demonstrates how to remove an event listener from an element when the button is clicked −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM removeEventListener()</title> <style> button{ padding: 8px; } </style> </head> <body> <p>Click Remove Listener to remove event handler.</h2> <br><br> <button class="myButton">Button 1</button> <button id="removeButton">Remove Listeners</button> <div id="output"></div> <script> const buttons = document.querySelectorAll('.myButton'); const output = document.getElementById('output'); const clickHandler = () => { output.textContent = 'Button clicked!'; }; buttons.forEach(button => button.addEventListener ('click', clickHandler)); document.getElementById('removeButton').addEventListener('click', () =>{ buttons.forEach(button =>button.removeEventListener ('click', clickHandler)); output.textContent='Event listeners removed!'; }); </script> </body> </html>
Example 2: Sets and Remove the Styles From a Div Element
Following is another example of the HTML DOM removeEventListener() method. This code contains two buttons. One button is used to add an event listener to an <div> element, and the other button is used to remove an event listener from that element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM removeEventListener()</title> <style> .highlighted { background-color: yellow; padding: 10px; border: 1px solid orange; margin-top: 10px; } button{ padding: 8px; } </style> </head> <body> <h1>HTML - DOM Element</h1> <h2>removeEventListener() Method</h2> <p>This example set and remove the styles by using the event handler..</p> <button id="hb">Highlight</button> <button id="rb">Remove Style</button> <div id="content">This is a div element.</div> <script> const ele = document.getElementById('content'); const hib = document.getElementById('hb'); const reb = document.getElementById('rb'); const highlightDiv = () => { ele.classList.add('highlighted'); }; const resetDiv = () => { ele.classList.remove('highlighted'); }; hib.addEventListener('click', highlightDiv); reb.addEventListener('click',function resetAndRemove(){ resetDiv(); hib.removeEventListener('click', highlightDiv); reb.removeEventListener('click', resetAndRemove); }); </script> </body> </html>
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
removeEventListener() | Yes | Yes | Yes | Yes | Yes |