ARTICLE

JQuery Drag and Drop using Visual Studio 2010

Posted by Sapna Articles | ASP.NET using VB.NET February 18, 2011
Learn about JQuery Drag and Drop which is used to convert group of elements into a list that can be sorted by dragging the items.
Download Files:
 
Reader Level:

Drag and Drop usually used to convert group of elements into a list that can be sorted by dragging the items. People generally use drag-and-drop on lists, tables, paragraphs or anything as long as you group the items.

In the following example we'll take a look at how to create drag-and-drop interfaces with jQuery. Use the following Google's CDN, to include jquery library and jquery UI plugin to your page.

<head>
...
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
...
</head>

Here we add a div to design gallery and then drag it to another div. You need to just call the draggable() method on it. Lets see the complete code.

<html>
<head>
<title>JQuery Drag and Drop</title>
<style>
   #makeMeDraggable { width: 202px;height: 140px;background: red; }
    .style1
    {
      color: #CC0099;
 
   }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script type="text/javascript">
    $(init);
    function init() {
       $('#makeMeDraggable').draggable();
    }
</script>
</head>
<body>
<div id="content" style="height: 400px;">
  <div id="makeMeDraggable" 
       style="border-style: ridge; border-color: #CC0099; background-color: #FFE8FF"> 
     <br />&nbsp;
     <
span class
="style1">
       <strong style="font-family: Verdana; font-size: small; font-weight: bold">
    
I am a Div, Drag me!</strong></span></div
>
</div>
</body>
</html>

End Result

drag&drop1.gif

Now drag the div with mouse drag into another div and see the output like below.

drag&drop2.gif

If you want highlight the dragged element add a CSS rule for the class ui-draggable-dragging in jquery UI. Let's do some modification so that div will changes from red to green while it's being dragged:

<style>
 
#makeMeDraggable { width: 300px; height: 300px; background: red; }
 
#makeMeDraggable.ui-draggable-dragging { background: green; }
</style>

Enjoy the JQuery Coding...

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor