How to check if list is sortable?

Viewed 11922

How to check if a specific list is sortable? Like $('#list').is(':sortable')... ?

If we will use

if ($('#list').sortable()) 

then the list will be made sortable again and not check if actually it is sortable.

7 Answers

I found in Safari (maybe others) the classes are not reliable. 'ui-sortable' remains on any item which has been made sortable even if you disable it, which results in the classes 'ui-sortable ui-sortable-disabled' both being used.

I found myself adding and removing the classes myself just to make sure they were as I expected.

if ($(this).hasClass("ui-sortable")) {
  $(this).removeClass("ui-sortable");
  $(this).sortable({ disabled: true });
}
else {
  $(this).addClass("ui-sortable");
  $(this).sortable({ disabled: false });
}

-- EDIT

Ok, so the classes are reliable but what I was after is not disabled but rather .sortable("destroy") which returns the element to it's state prior to initialisation (i.e removes all helper classes).

So for my sortable toggle (which is what I was creating):

if ($(this).hasClass("ui-sortable")) $(this).sortable("destroy");
else $(this).sortable();

You can check my answer here https://stackoverflow.com/a/57549308/4175975

Just call an instance of sortable, if return undefined, then is not loaded

if (typeof $("ul.sortable").sortable('instance') != 'undefined') {
    //$.ui.sortable is loaded and called
} else {
    //call sortable
}
Related