Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery + Sortable + live

I'm adding list items to a page dynamically with $.get, then appending them to the OL element. Pretty usual to this point.

But jQuery isn't aware of these new items when they get loaded into the page, and I'm not being able to make them sortable.

I made some trials with jQuery Live, but didn't get anywhere whit that...

like image 742
user202411 Avatar asked Nov 04 '09 07:11

user202411


2 Answers

Did you try .sortable('refresh')? http://docs.jquery.com/UI/Sortable#method-refresh

like image 158
Tommy Knowlton Avatar answered Nov 19 '22 18:11

Tommy Knowlton


The refresh method of .sortable() does not seem to recognize li's which are NOT added via the .sortable() functions.

Try adding your .sortable() initialisation code into a function which you call on document ready AND in you code where you dynamically add li's.

Instead of:

jQuery(document).ready(function() {
    jQuery("#mySortableOL").sortable({
        ...
    });
}
...
jQuery("#mySortableOL").append(...);
jQuery("#mySortableOL").sortable("refresh");

Try something like:

jQuery(document).ready(function() {
    jQuery("#mySortableOL").doSort();
}
...
jQuery("#mySortableOL").append(...);
doSort();
...
function doSort(){
    jQuery("#mySortableOL").sortable({
        ...
    });
}
like image 42
Pit Avatar answered Nov 19 '22 17:11

Pit