2

I'm looking for a way to de-initialise a jQuery plug-in after it's already been used on an element. It's for a slider, that after a certain action or after a certain situation arrises looses all the behavior applied to it byt he plugin.

Is this possible?

2
  • It depends on the plugin. Which one are you using? Commented Feb 27, 2013 at 17:33
  • It is the Flexslider plug-in. Commented Feb 28, 2013 at 9:28

1 Answer 1

6

It's only really possible if the plug-in provides a means of doing so. A well-written one should, usually by providing a "method" called destroy. For instance, in jQuery UI, you can remove a datepicker from an element like this:

$("selector").datepicker("destroy");

If the plug-in doesn't provide a means of doing it, it gets a bit tricky. You could clone the element without cloning events and data, and then replace the original with the clone, but that's unlikely to be effective in all cases and really is a hack workaround more than anything else.

Sign up to request clarification or add additional context in comments.

7 Comments

it stands to reason, then, that one could actually write a prototype function designed to destroy any $.fn.namespace, doesn't it?
@Kristian Destroying the namespace, and destroying the modifications that plugin made are very different.
@Kristian: Not following you. Removing a plug-in's effects on an element (and possibly its descendants) has little if anything to do with the $.fn object. When a plug-in is initialized on an element, typically it will add attributes, data, events, possibly new descendant elements (or ancestors, sometimes), possibly hide some existing descendants (or the element as a whole), etc., etc.
@Kristian: I believe that's the OP's question. Not about removing the plug-in from jQuery, but about undoing its effects on specific elements.
@shrewdbeans: Possibly. It would be very difficult and would require getting into undocumented areas of jQuery, and it would require assuming that the plug-in only did things via jQuery. There's no cross-browser way to know that something's done addEventListener, for instance, but if you dive deep into jQuery you can find information related to its on mechanism for adding handlers. In most cases, I think you'd be better off examining the source of the plug-in instead.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.