Created
March 21, 2018 18:30
-
-
Save johnwilson/8ef2b5b0b5d111705d31bb125cccd2ca to your computer and use it in GitHub Desktop.
Remove JQuery Plugin from DOM element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*----------------------------------------------------------- | |
| Original author: | |
| * http://ub4.underblob.com/remove-jquery-plugin-instance/ | |
------------------------------------------------------------*/ | |
var destroyPlugin = function($elem, eventNamespace) { | |
// check if plugin has a namespace | |
var isInstantiated = !! $.data($elem.get(0)); | |
if (isInstantiated) { | |
// remove all instances of plugin from element | |
// if plugin(s) doesn't(don't) have destroy method | |
$.removeData($elem.get(0)); | |
// Remove namespaced events added using .on() | |
$elem.off(eventNamespace); | |
// Remove namespaced events added using .bind() | |
$elem.unbind('.' + eventNamespace); | |
} | |
}; | |
// If plugin doesn't have a namespace | |
// then you have to find all events | |
// attached to DOM element | |
console.log( | |
'$element events:', | |
$._data($element.get(0), 'events') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment