How do I: Create sortable lists with one or more fixed elements.

January 22, 2009

When it comes to JavaScript libraries we are truly spoiled for choice. One of the best libraries that I enjoy using is JQuery. It has proven to be a lifesaver for a developer who doesn’t spend a lot of time coding in JavaScript.

Here’s a great way of using JQuery to create a sortable list with a single fixed element.

I set up my test file by adding JQuery functionality to my html file in the usual way:


$("ul").sortable({});
...
<ul>
	<li>red</li>
	<li>blue</li>
	<li>white</li>
	<li>black</li>
	<li>this li should not be sorted</li>
</ul>

By simply adding the option “items” with the value set to select all elements except the last like so:

$("ul").sortable( { items: "> li:not(:last)" } )

We are able to create a sortable list with a single fixed element.

More info:

Advertisements