Removing a Whole Set of Event Handlers - J Query

Problem
So, you’ve made a plugin-like block of code that binds many event handlers to certain DOM elements. Later, you want to clean them all up in order to dispose the plugin completely.This could get a little lengthy if you added many handlers. Maybe you don’t even have access to the bound handlers because they belong to another local scope.You can’t unbind every handler for a certain event (or any existing event), because you could be deleting other handlers that you didn’t take into account.

Solution

Use a unique name space for each plugin you make. Any handler bound within tippling must be added with this namespace.Later, when cleaning up, you just need to “unbind the whole namespace,” and all the related event handlers will go away with one single line of code.

Discussion

How to bind with a namespace?

To add a namespace to an event type, you simply add a . followed by the namespace name.
Since jQuery 1.3, you can add more than one (namespace) per event.
This is how you would bind the click and mouse down functions with a namespace:

How to clean up my plugin?

To dispose the bindings above, you would do:


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics