Introduction
I currently have a list (with ul
and li
's).
It's made sortable using jQuery:
$( "#sortable1" ).sortable({
update : function () {
var items = $('#sortable1').sortable('serialize');
alert(items);
}
});
$( "#sortable1" ).disableSelection();
The problem
This code moves the complete li
item (including it's class - and thus it's markup).
The question
Is there any way to match the target's destination class? I've setup a demo here: http://tinker.io/65292
So basically, when moving item 1
one place down, item 1
should become green and item 2
should become red
.
Additionally, when moving item 1
two places down, item 1
should be yellow, item 2
should be red and finally item 3
should be green.
This demo list only exists of four items, but realistically this could be any number.
Demo code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Sortable - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
#sortable li span { position: absolute; margin-left: -1.3em; }
.first { background-color: red; }
.middle1 { background-color: green; }
.middle2 { background-color: yellow; }
.last { background-color: blue; }
</style>
<script>
$(function() {
$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
});
</script>
</head>
<body>
<ul id="sortable">
<li class="first"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
<li class="middle1"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
<li class="middle2"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
<li class="last"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 4</li>
</ul>
</body>
</html>
Link: http://tinker.io/65292