Javatpoint Logo
Javatpoint Logo

How to Remove an Event Handler Using JavaScript Method

A state change of an object is referred to as an event in the javascript. When a browser does particular actions, then the HTML contains several events that display functions. When JavaScript is included in HTML and gives permission to run, JavaScript reacts to the occurrence of the event.

An event handler for a connected event can be removed from an element using the built-in JavaScript function removeEventListener(). If a button is disabled after being clicked once, for example, you may use removeEventListener() to remove the click event listener.

The removeEventListenert() method can be used in instances where you have generated an event, and there has been user interaction with it, but you want to remove the event from responding to the user after a specific case.

Syntax

The following syntax uses the javascript removeEventListenert() method to remove the event handler.

or

Parameters: The method requires two default parameters and one optional parameter. The parameter and its specifications are displayed below:

  • Event: This string identifies the type of event or event name that must be eliminated.
  • Handler: The job of the event handler is to eliminate.
  • UseCapture: it is a parameter that is optional.
    • By default, it has the Boolean value false, which indicates that the event handler should be removed from the bubbling phase.
    • If it is useCapture value sets to true, however, the removeEventListener() method should be used to remove the event handler from the capture phase.

Examples

The following examples show the operation of the remove event listener with the different event and handler.

Example 1: Remove the click event of the button

The basic example shows the javascript removeEventListenert() method with the event. The first time alert tab is popup after clicking the button. After that, remove the event functionality using the method and disable the event and its handler. The alert tab does not pop up when the second and after that button is clicked.

Output:

The output shows the button function to add and remove click events.

Before removing the event handler output

How to Remove an Event Handler Using JavaScript Method

After removing the event handler output

How to Remove an Event Handler Using JavaScript Method

Example 2: Remove the mousemove event

The example shows the value of the div tag of the orange background color. The mouse move event is active by default on the web page. After clicking on the function, the mouse move event does not work and displays the last value of the page.

Output:

The output shows the button function to add and remove the mouse move event. The image shows the after removing the event handler as an output.

How to Remove an Event Handler Using JavaScript Method

Example 3: Remove the mouseover event

In this example, we gave an element a mouseover event. When the user begins to hover over that element, they want the event to end. In order to remove the mouse hover event, we created removeEventListener() on the button element.

Output:

The output shows the button function to add and remove the mouse over the event.

How to Remove an Event Handler Using JavaScript Method

Example 4: Remove mouseover event with true useCapture

In this example, we use an element, a mouseover event, with the useCapture parameter. When the user begins to hover over that element, they want the event to end. If useCapture sets "true" data, then remove the event temporarily, and the function starts again.

Output

The output shows the button function to add and remove the mouse over the event.

Before removing the event handler output

How to Remove an Event Handler Using JavaScript Method

After removing the event handler output

How to Remove an Event Handler Using JavaScript Method

Example 5: Remove mouse move event with false useCapture

In this example, we use an element, a mouseover event, with the useCapture parameter. When the user begins to hover over that element, they want the event to end. If useCapture sets "false" data, then remove the event temporarily, and the function starts again. The default parameter sets false, but you can see the working functionality.

Output

The output shows the button function to add and remove the mouse move event.

Before removing the event handler output

How to Remove an Event Handler Using JavaScript Method

After removing the event handler output

How to Remove an Event Handler Using JavaScript Method

Web Browsers Supported

The list of supported browsers for the removeEventListenert() method of javascript is as follows:

  • Google Chrome 1.0
  • Firefox 1.0
  • Edge 9.0
  • Safari 1.0
  • Opera 7.0

Conclusion

If you have created an event and some event works continuously, then the removeEventListenert() method is used to stop functionality.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA