zack
zack

Reputation: 3198

jqueryUI Sortable: handling .disableSelection() on form inputs

example: i have an un-ordered list containing a bunch of form inputs.

after making the ul .sortable(), I call .disableSelection() on the sortable (ul) to prevent text-selection when dragging an li item.

..all fine but I need to re/enable text-selection on the form inputs.. or the form is basically un-editable ..

i found a partial solution @ http://forum.jquery.com/topic/jquery-ui-sortable-disableselection-firefox-issue-with-inputs

any thoughts?

Upvotes: 8

Views: 13901

Answers (7)

zeh
zeh

Reputation: 195

Quite old, but here is another way:

$('#my-sortable-component').sortable({
    // ...
    // Add all non draggable parts by class name or id, like search input texts and google maps for example
    cancel: '#my-input-text, div.map',
    //...
}).disableSelection();

Upvotes: 0

Asher Chayyim
Asher Chayyim

Reputation: 21

EASY! just do:

$( "#sortable_container_id input").click(function() { $(this).focus(); });

and replace "sortable_container_id" with the id of the element that is the container of all "sortable" elements.

Upvotes: 0

user2183078
user2183078

Reputation: 562

The following will disable selection for the entire document, but input and select elements will still be functional...

function disableSelection(o) {
  var $o = $(o);
  if ($o.find('input,select').length) {
    $o.children(':not(input,select)').each(function(x,e) {disableSelection(e);});
  } else {
    $o.disableSelection();
  }
}
disableSelection(document);

But note that .disableSelection has been deprecated by jquery-ui and will someday go away.

Upvotes: 0

intheweb
intheweb

Reputation: 529

A little improvement from post of Zack - jQuery Plugin

$.fn.extend({
    preventDisableSelection: function(){
        return this.each(function(i) {
            $(this).bind('mousedown.ui-disableSelection selectstart.ui-disableSelection', function(e) {
                e.stopImmediatePropagation();
            });
        });
    }
});

And full solution is:

$("#list").sortable({
  stop: function () {
    // enable text select on inputs
    $("#list").find("input").preventDisableSelection();
  }
}).disableSelection();

// enable text select on inputs
$("#list").find("input").preventDisableSelection();

Upvotes: 4

Edgar Ortega
Edgar Ortega

Reputation: 31

jQuery UI 1.9

$("#list").sortable();
$("#list selector").bind('click.sortable mousedown.sortable',function(e){
    e.stopImmediatePropagation();
});

selector = input, table, li....

Upvotes: 3

dzana
dzana

Reputation: 11

I had the same problem. Solution is quite simple:

$("#list").sortable().disableSelection();
$("#list").find("input").enableSelect();

Upvotes: 0

zack
zack

Reputation: 3198

solved . bit of hack but works! .. any comments how i can do this better?

apply .sortable() and then enable text-selection on input fields :


$("#list").sortable({
  stop: function () {
    // enable text select on inputs
    $("#list").find("input")
     .bind('mousedown.ui-disableSelection selectstart.ui-disableSelection', function(e) {
      e.stopImmediatePropagation();
    });
  }
}).disableSelection();

// enable text select on inputs
$("#list").find("input")
 .bind('mousedown.ui-disableSelection selectstart.ui-disableSelection', function(e) {
  e.stopImmediatePropagation();
});

Upvotes: 21

Related Questions