Get element starting position using jQuery ui sortable plugin

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

I want to see the 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 an element after dragging it with: -

ui.item.prevAll().length + 1

What do I use to get the starting position?

+5
source share
2 answers

Use event startand "cache" start 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));
        }
    });
})
+10
source
$(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 look at the console ...

+11
source

All Articles