kevinabraham kevinabraham - 3 months ago 25
Javascript Question

How to get moved ID and replaced ID in jquery sortable?

Im using jQuery sortable. I want to be able to get the ID for the item moved and the ID for the item it replaced. So far I am able to get the ID for the moved element but not the element it replaced.

My code is:

$(function () {
$("#sortable").sortable({
stop: function (event, ui) {
var moved = ui.item,
replaced = ui.item.prev();

// if replaced.length === 0 then the item has been pushed to the top of the list
// in this case we need the .next() sibling
if (replaced.length == 0) {
replaced = ui.item.next();
}

alert("moved ID:" + moved.attr("id"), "replaced ID:" + replaced.attr("id"));
}
});
});


How do I get back both the ID for the element being replaced and the element that was moved?

jsFiddle

Answer

Actually it works, you are just calling alert with wrong arguments; replace it with console.log or concatenate the strings like this:

alert("moved ID:" + moved.attr("id") + "replaced ID:" + replaced.attr("id"));

(I replaced , with +)

Comments