Get array of IDs after jQuery UI Sortable

html, javascript, jquery, jquery-ui

Solution

When you call `toArray`, you can pass an options object with an `attribute` field. This `attribute` field defines what attribute is used in the `toArray` call. For example:

var data = $(this).sortable('toArray', { attribute: 'predmet-id' });

That will give you an array of the `predmet-id` attributes of the items. See the `toArray` documentation.

Note that the default `attribute` is `id`, which is why your array returned empty strings before - none of your elements have `id` attributes!

Problem

I am using jQuery UI draggable and sortable functions and I am new to this. I have list of items in left (generated from database ) and I have boxes on right where user can drag and drop them. While/after dropping user can sort them. Here is full preview : http://jsbin.com/oBeXiko/3 Problem: How can I get array of IDs of elements in each box after sorting? I have tried .sortable("toArray") and sortable("serialize") but both return empty string. Simple HTML ``` <div id="raspored"> <div class="left"> <ul id="kanta">Delete</ul> <ul id="lista_predmeta" class="droptrue"> <li class="predmet ui-state-default" predmet-id="id_1">Item_1</li> <li class="predmet ui-state-default" predmet-id="id_2">Item_2</li> <li class="predmet ui-state-default" predmet-id="id_3">Item_3</li> <li class="predmet ui-state-default" predmet-id="id_4">Item_4</li> </ul> </div> <div class="right"> <ul class="raspored" razred="1">I</ul> <ul class="raspored" razred="2">II</ul> <ul class="raspored" razred="3">III</ul> <ul class="raspored" razred="4">IV</ul> </div> </div> ``` My jQuery functions ``` $(document).ready(function() { var url = ""; var raz = 0; $( ".raspored" ).sortable({ cursor: 'move', opacity: 0.65, stop: function ( event, ui){ var data = $(this).sortable('toArray'); console.log(data); // This should print array of IDs, but returns empty string/array } }); $(".raspored").droppable({ accept: ".predmet", drop: function(event, ui){ ui.draggable.removeClass("predmet"); ui.draggable.addClass("cas"); raz = $(this).attr("razred"); } }); $(".predmet").draggable({ connectToSortable:".raspored", helper: "clone", revert: "invalid", stop: function(event, ui){ var predmet = $(this).attr("predmet-id") ; console.log( "PredmetID = " + predmet + " | RazredID = " + raz); } }); $("#kanta").droppable({ accept: ".cas", drop: function(event, ui){ ui.draggable.remove(); } }); $( ".raspored, .predmet, .cas, #kanta, #lista_predmeta" ).disableSelection(); }); ```

Original source