user2459900
user2459900

Reputation: 13

JQuery ,each() with UI.sortable()

So I have an unordered list that's sortable using jQuery ui. I need the data-priority attribute to be 1-6 (or highest number) on every sort or re-sort. As it stands it sets it on load and doesn't re-set it after the sort.

I made a fiddle for you. It's probably something easy but I can't figure it out. >(

$("#priority-list").sortable(); 
    $("#priority-list li").each( function(i){
    $(this).attr("data-priority", "pri-" + (i + 1));
});

Thanks!

link for jsfiddle

Upvotes: 1

Views: 765

Answers (2)

som
som

Reputation: 4656

$("#priority-list").sortable({
    stop : function( event, ui ) {
         $("#priority-list li").each( function(i){
             $(this).attr("data-priority", "pri-" + (i + 1));
         });
     }
});

Upvotes: 0

Jason P
Jason P

Reputation: 27022

It looks like you aren't executing the code to do the prioritization after you sort.

http://jsfiddle.net/3CWBt/

$(function(){

    $("#priority-list").sortable({
        stop: setPriority
    });
    setPriority();

    function setPriority() {
        $("#priority-list li").each( function(i){
            $(this).attr("data-priority", "pri-" + (i + 1));
        });
    }

});

Upvotes: 2

Related Questions