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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: