Reorder Elements in a List Through Mouse Using jQuery

Introduction

 
This article explains how to reorder elements in a List using the mouse using jQuery.
 
What we are creating:
 
Reorder Elements using jQuery
 
Now let's see the procedure required to create such an application.
 
Step 1
 
First of all, I created an application in .NET.
 
In this application, I created a List of some objects that will be reordered.
  1. <body>  
  2.    
  3. <ul id="sort_me">  
  4.   <li >Item 1</li>  
  5.   <li >Item 2</li>  
  6.   <li >Item 3</li>  
  7.   <li >Item 4</li>  
  8.   <li >Item 5</li>  
  9.   <li >Item 6</li>  
  10.   <li >Item 7</li>  
  11. </ul>  
  12.    
  13. </body> 
The Id of this Unordered List is set to "sort_me".
 
Step 2
 
Now I will add some CSS Style to this Unordered List as in the following:
  1.   <style>  
  2. #sort_me { list-style-type: none; margin: 0; padding: 0; width: 60%; }  
  3. #sort_me li { margin: 0 3px 3px 3px; width:300px; border:solid 1px black; background-color:grey; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }  
  4. #sort_me li span { position: absolute; margin-left: -1.3em; }  
  5. </style> 
Step 3
 
Now for the main and final part of this application, in other words the jQuery part.
 
First of all you need to add the following two external jQuery files to your application:
  1. jQuery-ui-1.10.3.js
  2. jQuery-1.9.1.js
You can either download them from the jQuery site or you can download the Zip file of this application provided at the start of the article and then fetch the jQuery files from it.
Now we will create JavaScript functions that will work on our application.
  1. <script>  
  2.     $(function () {  
  3.         $("#sort_me").sortable();  
  4.         $("#sort_me").disableSelection();  
  5.     });  
  6. </script> 
This small piece of code will solve our problem and will show a great result.
 
The complete code of our application is as follows:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"   
  2. Inherits="WebApplication45.WebForm1" %>  
  3.    
  4. <!DOCTYPE html>  
  5.    
  6. <html xmlns="http://www.w3.org/1999/xhtml">  
  7. <head runat="server">  
  8.     <title></title>  
  9.     <script src="jquery-1.9.1.js"></script>  
  10.     <script src="jquery-ui-1.10.3.js"></script>  
  11.     <style>  
  12.   #sort_me { list-style-type: none; margin: 0; padding: 0; width: 60%; }  
  13.   #sort_me li { margin: 0 3px 3px 3px; width:300px; border:solid 1px black; background-color:grey; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }  
  14.   #sort_me li span { position: absolute; margin-left: -1.3em; }  
  15.   </style>  
  16.   <script>  
  17.       $(function () {  
  18.           $("#sort_me").sortable();  
  19.           $("#sort_me").disableSelection();  
  20.       });  
  21.   </script>  
  22. </head>  
  23. <body>  
  24.    
  25. <ul id="sort_me">  
  26.   <li >Item 1</li>  
  27.   <li >Item 2</li>  
  28.   <li >Item 3</li>  
  29.   <li >Item 4</li>  
  30.   <li >Item 5</li>  
  31.   <li >Item 6</li>  
  32.   <li >Item 7</li>  
  33. </ul>  
  34.    
  35. </body>  
  36. </html> 
Now our application is ready to be executed.
 
Output
 
First of all, you will see an output window like this one:
 
Reorder Elements using jQuery
 
Now you can reorder a list in any manner as you like.
 
Reorder Elements using jQuery