Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript window.close() Method
Next article icon

JavaScript removeEventListener() method with examples

Last Updated : 08 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Javascript removeEventListener() is an inbuilt function that is used to remove an event handler that was previously added using the addEventListener() function from the element.

Syntax:

element.removeEventListener(event, listener, useCapture)

Parameters:

It accepts three parameters which are specified below-

  • event: It is a string that describes the name of the event that has to be removed.
  • listener: It is the function of the event handler to remove.
  • useCapture: It is an optional parameter. By default, it is a Boolean value false which specifies the removal of the event handler from the bubbling phase and if it is true then the removeEventListener() method removes the event handler from the capturing phase.

Example 1: In this example, we will create a hover effect on some text and then remove the event using the removeEventListener() function.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <h3>Click this button to stop hovering effect !!</h3>
    <button id="clickIt" onclick="RespondClick()">
        Click here
    </button>
 
    <h1 id="hoverPara">Hover over this Text !</h1>
 
    <b id="effect"></b>
 
    <script>
        const y = document.getElementById("hoverPara");
 
        y.addEventListener("mouseover", RespondMouseOver);
 
        function RespondMouseOver() {
            document.getElementById("effect").innerHTML +=
                "mouseover Event !!" + "<br>";
        }
 
        function RespondClick() {
            y.removeEventListener("mouseover", RespondMouseOver);
            document.getElementById("effect").innerHTML +=
                'You clicked the "click here" button' + "<br>" +
                "Now mouseover event doesn't work !!";
        }
    </script>
</body>
 
</html>
 
 

Output: 

Note: If the triggered event needs to be found out, the type event property can be used. This property can be used to remove specific events by finding the type of the triggered event. 

Example 2: The event.type property can be used to display the type of the triggered event. Below JavaScript code shows the working of the event.type property and removeEventListener() method. 

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <button id="clickIt">Click here</button>
    <br>
 
    <b id="effect"></b>
 
    <script>
        const x = document.getElementById("clickIt");
 
        x.addEventListener("click", Respond);
 
        function Respond(e) {
            document.getElementById("effect").innerHTML +=
                "Type of event triggered = " + e.type + "<br><br>";
            document.getElementById("effect").innerHTML +=
                "Now click Event is disabled !! " + "<br><br>";
            x.innerText = "Click is disable !";
 
            // The click event is only one time triggered
            // by clicking the "click here" button.
            x.removeEventListener("click", Respond);
        }
    </script>
 
</body>
 
</html>
 
 

Output: 

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Next Article
JavaScript window.close() Method

V

vivekkothari
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-functions

Similar Reads

  • HTML DOM removeEventListener() Method
    The removeEventListener() method is used to remove an event handler associated with the addEventListener() method. This method of removing event handler can be used only with the addEventListener() method specified using an external function. Syntax: element.removeEventListener(event, function, useC
    2 min read
  • Javascript Window Open() & Window Close() Method
    The Javascript Window.Open() method is used to open the web pages into a new window or a new tab. It depends on the browser settings and the values assigned to the parameter. Syntax: window.open(URL, name, specs, replace)Parameters: This method accepts four parameters as mentioned above and describe
    3 min read
  • What is onerror() Method in JavaScript?
    The onerror() method in JavaScript is an event for error handling that occurs during the execution of code. It provides the mechanism to capture and handle runtime errors ensuring graceful error handling and debugging in the web applications. You can implement the onerror() method using the below me
    2 min read
  • JavaScript window.close() Method
    JavaScript window.close() method, is used for closing a certain window or tab of the browser which was previously opened by the window.open() method. Syntax: window.close();Parameters: This method does not accept any parameters. Example: In this example, at first we will use the window.open() method
    2 min read
  • jQuery | removeData() with Examples
    The removeData() is an inbuilt method in jQuery that is used to remove those data which are previously set with the data() method. Syntax:$(selector).removeData(args); Here "selector" is the selected element whose previously set data gets removed. Parameter: It accepts an optional parameter "args" w
    1 min read
  • JavaScript contextmenu MouseEvent
    When we click the right mouse button on our desktop, a menu-like box appears and this box is called the context menu. In JavaScript, a context menu event runs when a user tries to open a context menu. This can be done by clicking the right mouse button. This article demonstrates executing any operat
    1 min read
  • HTML DOM TokenList.remove() Method
    The TokenList.remove() Method in HTML DOM is used to remove the particular nodes or tokens from a NodeList. It is an in-built method of TokenList Object. Syntax: remove(token); remove(token, token); remove(token, token, token); ... Parameter Values: It contains a string value that represents the nam
    1 min read
  • Bulma Notification JavaScript Example
    Bulma is a CSS framework based on flexbox. It can be very helpful to develop websites easily as it comes with pre-styled elements and components so you don't have to build everything from the ground up. In this article, we will be seeing how to make a notification disappear with the help of some Jav
    2 min read
  • Web API Selection.removeRange() Method
    The Web Selection API gives developers the ability to recognize the screen regions that the user has now selected and to use code to initiate user selections. The Selection.removeRange() method removes a range from a selection. Syntax: removeRange(range);Parameters: This method has one parameter onl
    2 min read
  • How to Remove Event Handlers in JavaScript ?
    In JavaScript, event handlers are functions attached to HTML elements to respond to specific events like clicks or mouse movements. Removing event handlers effectively detaches the specified function from listening to that particular event on that element. Table of Content Using removeEventListener(
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences