In this shot, we will learn how to use the removeEventListener()
method to remove an event handler from the element.
removeEventListener()
method takes 2 parameters:
Let’s write code to remove an event and its handler.
Remove Event
button calls the remove()
function.btn1
. We will add and remove the event in this element.p
tag with id demo1
, which will display the count. The text will change when we call the counter()
function.btn1
, which will call the counter()
function.counter()
function is called whenever the +++
button is clicked, and the text of the p
element will change to new text.remove()
function removes the event from the button with id btn1
. We pass the name of the event and its function to removeEventListener()
to remove the event.When we click the +++
button, the counter works, but after we click the Remove Event
button, the counter stops working, which means the event is removed from the +++
button.