On the road again...

The life of a .NET commuter.

Drag – Drop Interaction with jQuery

A few days ago, I posted about getting started with AJAX, Facebox and jQuery. Well, I've been doing a little more work with it, and wanted to do a quick demo of the Drag and Drop interaction that is possible with jQuery, and a little preview of how to deal with this stuff and AJAX. It's pretty slick.

First of all, go and get the jQuery library, the jQuery UI library. Add script tags that point to the jQuery Library, and then the ui.draggable.js, ui.draggable.ext.js, ui.droppable.js, ui.droppable.ext.js, and ui.mouse.js files from the jQuery UI library.

Then, drop two divs on a page. Give them two different id's.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>Untitled Page</title>

<script src="../JS/jquery-1.2.2.pack.js"></script>

 

<script type="text/javascript" src="../JS/jquery.ui-1.0/ui.mouse.js"></script>

<script type="text/javascript" src="../JS/jquery.ui-1.0/ui.draggable.js"></script>

<script type="text/javascript" src="../JS/jquery.ui-1.0/ui.draggable.ext.js"></script>

<script type="text/javascript" src="../JS/jquery.ui-1.0/ui.droppable.js"></script>

<script type="text/javascript" src="../JS/jquery.ui-1.0/ui.droppable.ext.js"></script>

 

<style>

#divDrop

{

margin-left:auto;

width:250px;

height:250px;

background-color:powderBlue;

}

#divDrag

{

width:100px;

height:100px;

position:absolute;

top:0px;

left:0px;

background-color:yellow;

}

.droppable-active {

     opacity: 1.0;

     background-color:yellow;

}

.droppable-hover {

     outline: 1px dotted black;

     background-color:white;

}

</style>

<script>

$(document).ready

(

function()

{

$("#divDrag").draggable({helper: 'clone'});

$("#divDrop").droppable({

accept: "#divDrag",

activeClass: 'droppable-active',

hoverClass: 'droppable-hover',

drop: function(ev, ui) {

     $(this).append("<br>Dropped!");

     var theId = $(this).attr("id");

     alert(theId);

     //Do your AJAX stuff in here.

}

});

 

}

);

</script>

 

</head>

<body>

<div id='divDrop'>Drop Here.</div>

<div id='divDrag'>Drag Me.</div>

</body>

</html>

 

That's the gist of it. As you can see, the possibilities now are endless. What's really impressive is drag and drop in well, as little as two lines of code.

Enjoy!

Posted: Jan 25 2008, 11:08 PM by du8die | with 6 comment(s)
Filed under: , ,

Comments

Karthik said:

can u show an example? The code is not working...

# February 5, 2008 7:00 AM

Tom said:

A link to a working example would be nice.

# February 13, 2008 3:54 PM

du8die said:

I'm working on getting this up and running.  Hopefully later tonight.

# February 13, 2008 4:35 PM

Tom said:

Would love to see an example of this code as I want to use it on my site, I'm going to have a play though and see if I can get it to work. I've been looking at the Interface stuff and it looks rather heavy for what I want but we'll see.

Thanks for the tips.

# February 20, 2008 5:13 AM

Tom said:

Sorry if it's like I'm spamming this blog, but I found the answer!

var theId = $(ui.draggable.element).attr("id");

Feel free not to display all these comments, I won't be offended :)

Thanks for writing the article though, really helped me on my way to getting this sorted.

# February 20, 2008 5:50 AM

Phoebe Bright said:

Thanks for putting me on the right track.  I used:

var theId = $(ui.draggable[0]).attr("id");

# October 3, 2009 8:06 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)