Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-enable/re-bind jQuery UI Sortable after $.unbind?

$('div.something').sortable(options) works fine, but breaks after $('div.something').unbind();. Attempting to re-run $('div.something').sortable(options); or $('div.something').sortable('refresh'); after $('div.something').unbind(); does not help.

I am using $.unbind to deactivate/un-initiate a plugin by removing the events from the element that the plugin is being applied on, however this technique is having an adverse effect in that it is breaking $.sortable. Any ideas on how re-activate sortable?

I am using the latest versions of jQuery and jQuery UI.

like image 362
Steve Avatar asked Mar 17 '13 03:03

Steve


1 Answers

Making a call to .sortable('destroy') prior to calling .unbind() will completely remove the sortable feature from the element.

This ensures proper tear-down before performing the somewhat dangerous call to .unbind(), which removes all bound handlers on the element. Then, you can re-initialize the sortable feature.

// Initialization of the sortable feature
$('div.something').sortable(options);
...
// Remove the sortable feature to prevent bad state caused by unbinding all
$('div.something').sortable('destroy');
// Unbind all event handlers!
$('div.something').unbind();
...
// Re-initialize the sortable feature
$('div.something').sortable(options);
like image 195
Ruben Infante Avatar answered Sep 19 '22 02:09

Ruben Infante