Published on: 02 Jun, 2013

Jquery drag and drop example

Tumblr Digg Google Plus

In this tutorial you will learn how to create Drag and Drop in JQuery. You will see how the quick demo of drag and drop works below. Further reading, you will find quick tutorial with code comments to explain how everything works.

Quick Demo

You will be able to see the quick demo in here:



Before starting the tutorial, you will need to import the required javascripts as below:

<script src="" type="text/javascript"></script>

<script src="" type="text/javascript"></script>
Note: You can save the scripts on your local server if you prefer, by copying the links and paste them to your browser.

Next let's see the html code.

<div id="boxdemo">
	<div class="draggable">

	<div class="droppable"></div>
	<div class="clear"></div>

This will be the CSS Stylesheet

/** This is the wrapper box that the drag and drop box is not allowed to pass outside of this box.**/
	border:solid 1px blue;

/** style for droppable content **/
.droppable { 
	width: 150px; 
	height: 100px;
	float: left; 
	border:solid 1px gray;

/** style for draggable content **/
	float: left; 

/** style when an object is moving hover **/

/** style for droppable when the moveable object is dropped **/

/** clear the float used by other draggable and moveable objects **/

This will be the jquery and javascript function.

$(function() {
        /** we set the draggable class to be draggable, we add the containment which will be #boxdemo, so dropable and draggable object cant pass out of this box **/
	$( ".draggable" ).draggable({ 
		revert: "invalid"

	$( ".droppable" ).droppable({
        /** tolerance:fit means, the moveable object has to be inside the dropable object area **/
		tolerance: 'fit',
		over: function(event, ui) {
            /** We add the hoverClass when the moveable object is inside of the dropable object **/
		out: function(event, ui) {
            /** We remove the hoverClass when the moveable object is outside of the dropable object area **/
        /** This is the drop event, when the draggable object is moved on the top of the dropable object area **/
		drop: function( event, ui ) {
			$( ".droppable" ).addClass('dropClass');

Attribute Properties Explanation

Name Explanation
revert When the value is set to true, the element will return to its start position when the drag process stops. There are two possible string values which are 'valid' or 'invalid'. If the value is set to invalid, revert will only occur if the draggable has not been dropped on a droppable. While for the valid value, it will perform the other way around.
tolerance Those are the values of tolerance: 'fit', 'intersect', 'pointer', 'touch'.
  • fit: draggable overlaps the droppable entirely
  • intersect: draggable overlaps the droppable at least 50%
  • pointer: mouse pointer overlaps the droppable
  • touch: draggable overlaps the droppable any amount

If you want to see the quick demo on how drag and drop works using JQuery, please click this link.


There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.