jqueryUI Sortable: handling .disableSelection() on form inputs
Asked Answered
A

7

8

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?

Appertain answered 26/10, 2010 at 19:15 Comment(3)
If you could post your code, I could probably make this work... have you looked at the cancel option of sortable? Consider making a JSFiddle that explains it. (www.jsfiddle.net)Labaw
@ Fosco. thks. been a bit busy lately.. i just posted a hack that solves this ..Appertain
As a remark, it not only affects 'inputs' but 'selects' too.Niue
A
21

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();
});
Appertain answered 15/11, 2010 at 19:23 Comment(0)
O
4

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();
Ontario answered 30/11, 2012 at 8:35 Comment(2)
This works everywhere since jQuery is crossplatform :) What exactly doesn't works?Ontario
Text input is not selectable.Burglary
H
3

jQuery UI 1.9

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

selector = input, table, li....

Hindbrain answered 18/10, 2012 at 17:3 Comment(1)
sounds great! is this documented, do you have a link? or a jsFiddle ? ..not that I'm skepticalAppertain
T
0

I had the same problem. Solution is quite simple:

$("#list").sortable().disableSelection();
$("#list").find("input").enableSelect();
Theodoratheodore answered 5/10, 2011 at 13:35 Comment(3)
I dont see jQuery docs for 'enableSelect()'.. Please correct me if I'm wrong, but I dont believe it exists!Appertain
@Appertain it was probably a typo. Fixed.Obi
The correct name is enableSelection(). See api.jqueryui.com/enableSelectionShowiness
A
0

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.

Ammieammine answered 5/7, 2013 at 16:48 Comment(0)
G
0

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.

Goldarn answered 28/10, 2014 at 20:50 Comment(1)
This question is from 2010. Answers here are very unlikely to be read.Synergistic
R
0

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();
Renferd answered 9/5, 2019 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.