Aakash Chakravarthy
Aakash Chakravarthy

Reputation: 10701

jquery deactivate plugin

Consider I have a plugin "fooPlugin" I attach it to an element like $(element).fooPlugin(); But later, i want to deactivate the plugin. How to deactivate the plugin? Currently, I am using jwysiwyg plugin

I am creating a editor with jwysiwyg plugin and I need to enable the editor when a button is clicked and remove the editor when another button is clicked

Please help!

Upvotes: 2

Views: 1276

Answers (4)

Vin
Vin

Reputation: 2165

As mentioned by others, there isn't a standard way of doing this as the plugin could be doing all manner of things (and all plugins are different). You will need to edit the plugin.

I have come across this very same problem and found that this works for very simple plugins:

Give the body element a class e.g. ".pluginActivated". Edit the plugin so that before it does anything, it checks if the body has that class.

Now you can remove the .pluginActivated class from the body and the plugin will hopefully stop working on the element.

e.g. the plugin may start with

return this.each(function(){

Just add an if function directly underneath, before any code where the plugin starts doing stuff that you want to 'deactivate':

if ($("body").hasClass("pluginActivated")){

(and remember to close off the curly brackets with a } at the end).

Now this has other issues: the plugin is not truly deactivated, it will still check for the body class when fired, but at least it may have the desired effect of not being bound to your specified element.

Upvotes: 0

C. E.
C. E.

Reputation: 10627

I haven't looked at the plugin specifically, but what plugins usually do is that they modify the markup and then attach event listeners to it.

For example, a content editor may create a few buttons like "bold", "italic" etc. and then attach a click event to each button. At a click, it will add [b] ... [/b] to a textarea for example which is also given by the defined markup.

What you can do to "deactivate" this plugin, then, is to reset the markup. To do this, first use the jquery remove function to remove all markup that has been created (it should have been put inside a container so it should be easy and quick) and then append again the required markup that you had from the beginning. This markup will not be affected by whatever listeners had been created earlier.

Upvotes: 0

morgancodes
morgancodes

Reputation: 25285

I don't think you'll find a universal "deactivate" technique that works across all plugins. jwysiwyg probably attaches event handlers to $(element) and would need to implement its own "deactivate" method. Otherwise, you could open up the source and see which even handlers it attaches and then remove them by hand.

Upvotes: 0

Anurag Uniyal
Anurag Uniyal

Reputation: 88865

There can not be a general method to detach a plugin, who knows what that plugin might have done to nodes, so only that plugin can detach it self e.g.

$(element).fooPlugin() //add
$(element).removeFooPlugin() //remove

So see jwysiwyg docs or ask them, looks like they haven't implemented any destroy or remove methods.

Upvotes: 1

Related Questions