manix manix - 2 years ago 117
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:


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
Item 3
somehow I'd need to get a
. As you can see below,
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