5

I am using the jQuery UI sortable plugin and I am trying to get 2 alerts

I want the staring position of the element and the finished position of the element.

$(function() {
    $("#filterlist ul").sortable({ opacity: 0.6, cursor: 'move', update: function(event, ui) {
            alert(ui.item.prevAll().length + 1);
        }
    });
});

I can get the position of the item after it has been dragged by using:-

ui.item.prevAll().length + 1

What do I use to get the position it started from?

Rippo
  • 22,117
  • 14
  • 78
  • 117
  • Duplicate of: http://stackoverflow.com/questions/2442232/getting-the-position-of-the-element-in-a-list-when-its-drag-dropped-ui-sortable – Mottie Apr 12 '10 at 13:28

2 Answers2

11
$(function() {

    $("#sortable").sortable({
       start: function(event, ui) { console.log('before @ '+ ui.item.index()) },
       update: function(event, ui) { console.log('now @ '+ ui.item.index()) }
    });

});​

try this demo and watch at the console...

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
10

Use the start event and "cache" the starting position

var start;
$(function() {
    $("#filterlist ul").sortable({
        opacity: 0.6,
        cursor: 'move',
        start: function(event, ui) {
            start = ui.item.prevAll().length + 1;
        },
        update: function(event, ui) {
            alert(start + " -> " + (ui.item.prevAll().length + 1));
        }
    });
})
jitter
  • 53,475
  • 11
  • 111
  • 124