Tyssen
Tyssen

Reputation: 1711

Getting order of sortable list in form input

I'm using HTML5 Sortable to sort an ordered list of items, and when the change is made, I'd like to capture that order in a hidden form input.

With what I have below, the value of the hidden input is [object HTMLOListElement].

What do I need to do to convert that into the values of the list items instead?

$('.sortable').sortable().bind('sortupdate', function() {
    var order = $('.sortable').toArray();
    $('#statements').val(order.join(','));
});

Upvotes: 1

Views: 927

Answers (1)

SarathSprakash
SarathSprakash

Reputation: 4624

Try this

$(".sortable").sortable({
    stop: function(event, ui) {
        var data = "";

        $(".sortable").each(function(i, el){
            var ord = $(el).text();
            data += ord+"="+$(el).index()+",";
        });

        $('#statements').val(data);
    }
});

Hope this helps,Thank you

Upvotes: 1

Related Questions