List Reorder

A jQuery plugin by Jordan Bach.

Download List Reorder

List Reorder is a jQuery plugin that allows you to reorder any simple ordered or unordered list. List Reorder is easy to use and does not require any additional markup. Its look and feel is completely customizable using a set of CSS classes. It is distributed under the terms of the MIT license.

Obviously, List Reorder requires the jQuery Javascript library because it is a jQuery plugin. Additionally, it requires the Disable Text Select plugin. For your convenience, both are packaged with this plugin along with the HTML and CSS sources for this example page. To use List Reorder on your own pages, you must first include the jQuery library, the Disable Text Select plugin, the List Reorder plugin, and your custom code in that order.

A simple example

Here we make two unstyled lists reorderable. You select the list as you would select any other DOM node using jQuery's CSS selector capabilities. Also, you must specify styles for the drag handle or it will not be visible. If it is not visible, you can't reorder your list! Optionally you could set the useDefaultDragHandle option to true. Click-drag the gray box to the right of the list items to reorder the list.

  1. Armadillo
  2. Bobcat
  3. Cougar
  4. Dog
  5. Eagle

The Javascript Code

$(document).ready(function() {	
	var unordered = $('ul#list1').ListReorder();
	var ordered = $('ol').ListReorder();
});
	
Use CSS to make the drag handle visible.
.dragHandle {
	display: block;
	float: right;
	width: 10px;
	height: 10px;
	border: 2px solid #06f;
	background:#fff;
	cursor: move;
}
	

* It is important to specify a background color/image for the drag handle, otherwise Internet Explorer will not detect mouseover or mouseclick events on the element.

Multiple Lists with Event Handling and Custom Styles

In this example we override the default options so we can use our custom CSS classes. You can style the lists almost any way you want, but the list items must be arranged vertically (for now). Also notice that we're using the default dragHandle for this example.

Next we bind an event handler function to the "listorderchanged" event. Whenever the order of either list changes, the output in the box below the lists is updated, showing the new order. The list order is given as an array where the value for each index corresponds to the original position of that item in the list. Clicking on either of the reset buttons will restore the items in that list to their original order.

Event Handler Output

Javascript Code

$(document).ready(function() {
	var options = {
		itemHoverClass : 'myItemHover',
		dragTargetClass : 'myDragTarget',
		dropTargetClass : 'myDropTarget',
		useDefaultDragHandle : true
	};
	
	var lists = $('.lists').ListReorder(options);
	
	lists.bind('listorderchanged', function(evt, jqList, listOrder) {
		var str = jqList.attr('id') + " order changed: \n"
				+ "\tcurrent -- original\n";
		for (var i = 0; i < listOrder.length; i++)
			str += "\t" + i + " -- " + listOrder[i] + "\n";
		
		$('#example2out').html(str);
	});
	
	$('#example2 input').click(function(){
		if ($(this).attr('value') == 'Reset List A')
			lists.get(0).restoreOrder();
		else
			lists.get(1).restoreOrder();
		return false;
	});
});
	

Custom CSS Styles

ul.lists {
	list-style: none;
	padding: 0;
	border-left: 3px solid #06f;
	border-top: 1px solid #999;
}

.lists li {
	margin: 0;
	padding: 10px 3px;
	border: 1px solid #999;
	border-top: 0;
	border-left-color: #06f;
	background: #f6f6f6;
}

.myItemHover {
	background: #cef;
}

.myDragTarget {
	background: #cef;
	opacity: 0.7;
	border: 2px dashed #f90;
	padding: 10px 3px; /* matches '.list li' padding */
}

.lists .myDropTarget {
	background: #ccc;
}