kevinabraham kevinabraham - 3 months ago 14
HTML Question

How to use jQuery sortable to update div ID's when a user moves an element?

I am using jQuery sortable to allow users to move elements around on a page. Each div element has a unique ID. When the user swaps an element on the page with another I want to also swap those element ID's and alert the new list of ID's order, So I can save this new order to the backend.

I am struggling to get the child element ID's for when the user moves an element. My jQuery so far is:

$(function() {
$("#sortable").sortable({
stop: function(ev, ui) {
var children = $('#sortable').sortable('refreshPositions').children();
alert('Positions: ');
$.each(children, function() {
alert($(this).attr('id'));
});
}
});
$("#sortable").disableSelection();
var drag_id = $('.container').attr("id");
});


but the alert message is always
undefined
. How do I swap the div ID's when the user makes a change and save this to an array and then
alert
this array?

FIDDLE

Answer

You must set ID for (li) tag . Check Fiddle now

 [fiddle](https://jsfiddle.net/7rbv20yd/5/)
Comments