Skip to content

Instantly share code, notes, and snippets.

@johnwilson
Created March 21, 2018 18:30
Show Gist options
  • Save johnwilson/8ef2b5b0b5d111705d31bb125cccd2ca to your computer and use it in GitHub Desktop.
Save johnwilson/8ef2b5b0b5d111705d31bb125cccd2ca to your computer and use it in GitHub Desktop.
Remove JQuery Plugin from DOM element
/*-----------------------------------------------------------
| 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