From the course: JavaScript: Enhancing the DOM
Unlock the full course today
Join today to access over 24,000 courses taught by industry experts.
Removing event handlers
From the course: JavaScript: Enhancing the DOM
Removing event handlers
- [Instructor] Assigning event handlers is just one part of the equation. It's equally important to know how to remove them. This can be required 'cause you only want the handler to run runs, for example, or until a certain condition is met. Depending on your exact situation, removing an event listener can prevent memory leaks and all sorts of unwanted behavior. How to do this? Well, it's rather intuitive. We can use removeEventListener for this purpose. It's crucial that the function you remove is the same reference as the one you edit. So correct, you cannot do this with error functions or anonymous functions unless they are declared in a scope that's accessible to both the Add Listener and Remove Listener call. Here we have a simple page with a button that performs a one-time action. After we're done loading the content, we go ahead and select the action button. We then add the event listener for click to this and we specify the function, handleActionOnce. And in this function, we…
Contents
-
-
-
-
-
-
-
-
(Locked)
Event listeners, event handlers, and event objects6m 26s
-
(Locked)
Common DOM events3m 38s
-
(Locked)
Removing event handlers2m 22s
-
(Locked)
Event propagation: Capturing and bubbling4m 49s
-
(Locked)
Event delegation1m 39s
-
(Locked)
Working with preventDefault2m 30s
-
(Locked)
Challenge: Eventful app53s
-
(Locked)
Solution: Eventful app1m
-
(Locked)
-
-
-
-