How do I disable a jquery-ui draggable?

Jquery Ui

Jquery Ui Problem Overview


How do I disable a jQuery draggable, e.g. during an UpdatePanel postback?

Jquery Ui Solutions


Solution 1 - Jquery Ui

Could create a DisableDrag(myObject) and a EnableDrag(myObject) function

myObject.draggable( 'disable' )

Then

myObject.draggable( 'enable' )

Solution 2 - Jquery Ui

To temporarily disable the draggable behavior use:

$('#item-id').draggable( "disable" )

To remove the draggable behavior permanently use:

$('#item-id').draggable( "destroy" )

Solution 3 - Jquery Ui

To enable/disable draggable in jQuery I used:

$("#draggable").draggable({ disabled: true });			

$("#draggable").draggable({ disabled: false });

@Calciphus answer didn't work for me with the opacity problem, so I used:

div.ui-state-disabled.ui-draggable-disabled {opacity: 1;}

Worked on mobile devices either.

Here is the code: http://jsfiddle.net/nn5aL/1/

[tag:enable][tag:disable][tag:draggable][tag:jQuery][tag:opacity][tag:problem][tag:html]

Solution 4 - Jquery Ui

It took me a little while to figure out how to disable draggable on drop—use ui.draggable to reference the object being dragged from inside the drop function:

$("#drop-target").droppable({
    drop: function(event, ui) {
        ui.draggable.draggable("disable", 1); // *not* ui.draggable("disable", 1);
        …
    }
});

HTH someone

Solution 5 - Jquery Ui

Seems like no one looked at the original documentation. May be there was no it at that time))

Initialize a draggable with the disabled option specified.

$( ".selector" ).draggable({ disabled: true });

Get or set the disabled option, after init.

//getter
var disabled = $( ".selector" ).draggable( "option", "disabled" );
//setter
$( ".selector" ).draggable( "option", "disabled", true );

Solution 6 - Jquery Ui

In the case of a dialog, it has a property called draggable, set it to false.

$("#yourDialog").dialog({
    draggable: false
});

Eventhough the question is old, i tried the proposed solution and it did not work for the dialog. Hope this may help others like me.

Solution 7 - Jquery Ui

The following is what this would look like inside of .draggable({});

$("#yourDraggable").draggable({
	revert: "invalid" ,
	start: function(){ 
        $(this).css("opacity",0.3);
    },
    stop: function(){ 
        $(this).draggable( 'disable' )
    },
	opacity: 0.7,
	helper: function () { 
		$copy = $(this).clone(); 
		$copy.css({
			"list-style":"none",
			"width":$(this).outerWidth()
		}); 
		return $copy; 
	},
	appendTo: 'body',
	scroll: false
});

Solution 8 - Jquery Ui

I have a simpler and elegant solution that doesn't mess up with classes, styles, opacities and stuff.

For the draggable element - you add 'start' event which will execute every time you try to move the element somewhere. You will have a condition which move is not legal. For the moves that are illegal - prevent them with 'e.preventDefault();' like in the code below.

    $(".disc").draggable({
        revert: "invalid", 
        cursor: "move",
        start: function(e, ui){                
            console.log("element is moving");
            
            if(SOME_CONDITION_FOR_ILLEGAL_MOVE){
                console.log("illegal move");
                //This will prevent moving the element from it's position
                e.preventDefault();
            }    
            
        }
    });

You are welcome :)

Solution 9 - Jquery Ui

Change draggable attribute from

<span draggable="true">Label</span>

to

<span draggable="false">Label</span>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErnieStingsView Question on Stackoverflow
Solution 1 - Jquery UimadcolorView Answer on Stackoverflow
Solution 2 - Jquery UiPhilBView Answer on Stackoverflow
Solution 3 - Jquery UiCarinaPilarView Answer on Stackoverflow
Solution 4 - Jquery UiOli StudholmeView Answer on Stackoverflow
Solution 5 - Jquery Uimakaroni4View Answer on Stackoverflow
Solution 6 - Jquery UiAndrei CView Answer on Stackoverflow
Solution 7 - Jquery UimaudulusView Answer on Stackoverflow
Solution 8 - Jquery UiCombineView Answer on Stackoverflow
Solution 9 - Jquery UiddagsanView Answer on Stackoverflow