manix manix - 1 year ago 84
jQuery Question

ui.helper index in stop() function

Trying to get the index in the following example is driving crazy. I am using this base code:

http://jqueryui.com/draggable/#sortable

On

stop()
event, I am trying to know what is the index of the new element dragged. For example:

[Drag me down]
-------------
[Item 1]
[Item 2]
[Item 3]
[Item 4]


If I release the new item between
Item 2
and
Item 3
somehow I'd need to get a
2
. As you can see below,
ui.helper.index()
is not valid, because it is returning the index of the original element ([Drag me down] element) :

$( ".sortable" ).sortable({
// ...
receive: function( event, ui ) {
var indexAtReceive = $(this).data("ui-sortable").currentItem.index();
}
});

$( ".draggable" ).draggable({
// ...
stop: function( event, ui ) {
// Here indexAtStop is the index of the original element,
//but not the new dragged element
var indexAtStop = ui.helper.index();
}
});


Let me know if you need to more relevant code or test. Thank you in advance.

Answer Source

you want ui.item.index() in this case inside your stop function

$( ".draggable" ).sortable({
    // ...
    stop: function( event, ui ) {
        var indexAtStop = ui.item.index();
    }
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download