The widget is not destroying because there is no call to the widget factory destroy()
function.
In your jsFiddle, each time you click on the "Show Widget" button, you :
- first remove the div
#target
content - then call the widget with no param.
Calling the widget with no params multiple times result in :
- first time : call the factory
create()
function (which internally call the user defined_create()
function, but you don't have some), then call the factoryinit()
function (which internally call the user defined_init()
function) - all other times : only call the
init()
function
The problem is that you are binding events in the _init()
function, so new events are binded each time you call the plugin, that is to say each time you click on your buttons. Plus you bind a click event (to call your plugin) using button
selector, so the event is binded on the 2 buttons.
You should define a _create()
function which will be called only once, to define actions needing to be done only once, such as event binding.
Have a look in this jsFiddle, it should fit your needs. I simplified a little your code and add some explanations.