Disable, enable and destroy the draggable object in JavaScript

Description

The following code shows how to disable, enable and destroy the draggable object.

Example


<!--   ww  w  . ja v a 2  s  .c  o  m-->


<html lang="en">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript">
$(function() {
$("#drag").draggable();

//define function to toggle draggability
function toggle(action) {
(action == "destroy") ? $("#drag").draggable(action).removeClass("drag") : $("#drag").draggable(action);
}

//define click handler for buttons
$("button").click(function() {
toggle($(this).attr("id"));
});
});
</script>
</head>
<body>
<button id="disable">Disable</button>
<button id="enable">Enable</button>
<button id="destroy">Destroy</button>
<div id="drag">asdf</div>


</body>
</html>

Click to view the demo

The code above generates the following result.

Disable, enable and destroy the draggable object in JavaScript