kevinabraham kevinabraham - 10 months ago 60
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 () {
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 =;

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?



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 +)