Here is a working example using the start
callback.
You can use the allowdrag
checkbox to enable/disable the dragging functionality:
$(document).ready(function () {
$("#draggable").draggable({
revert: "invalid",
start: function(e) {
if (!$('#allowdrag').is(':checked')) {
e.preventDefault();
}
}
});
$("#Dropable").droppable({
activeClass: "ui-state-highlight",
drop: function (event, ui) {
alert("dropped!");
},
tolerance: 'fit'
});
});
#draggable {
width: 150px;
height: 150px;
padding: 0.5em;
}
#Dropable {
height:300px;
width:400px;
border: 2px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
<input type="checkbox" id="allowdrag" /><label for="allowdrag"> Allow drag</label><br />
<div id="draggable" class="ui-widget-content">
<p>Drag me around</p>
</div>
<div id="Dropable">Droppable area</div>
You can change the if (!$('#allowdrag').is(':checked')) {
part to whatever you want to check in order to enable/disable the drag option.