7

In JqueryUI sortable, When the user drags a item, change event called. I got this on below example.

jsFiddle code here

stop: function(event, ui) {
        console.log("New position: " + ui.item.index());
    },
    change: function(event, ui) {
        // When the item postion is changed while dragging a item, I want it's position
        console.log("New position: " + ui.item.index());
    }

In my code When the user drags a item (Item7) and changes the position , while dragging a item and changing the position . I want to get the position of a dragging item.

I just saw a answer here : jQuery UI Sortable Position

In the above link, position value (ui.item.index()) is accessed on Stop function, while dragging a item, I want it's position (on change function) ? Any Idea ?

I need this because when the user tries to place a item7 after a item2, I will do some validation getting values from previous Item2 (If I know the current position of the dragging item, Then only I can access previous item) . If the validation is success, It can be placed or I have to show some message.

1

2 Answers 2

8

If you want to get the index of what's currently hovered, try using placeholder.

change: function(event, ui) {
      console.log("New position: " + ui.placeholder.index());
}

FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

0

Jude Duran's answer will not work constantly (found this out after taring my hair out). The first and last items placeholder move depending on the mouse location.

function getPaceholderIndex( ui ) {
    var clone = $(myMenu).clone();
    $('.ui-sortable-helper', clone).remove();
    return $('.ghost', clone).index();
}

will work more efficiently as it firstly removes the actual item and leaves the placeholder. Note that I have 'ghost' set as the placeholder parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.