If you destroy sortable every time before initializing this element, it will work:
function init(e) { // First of all - we destroy the sortable $('ul').sortable('destroy'); var root = $('<ul></ul>') createNodes(root) e.html(root) root.sortable({ group: 'foo', onDrop: function ($item, container, _super, event) { // These two lines are default behaviour of the plugin $item.removeClass(container.group.options.draggedClass).removeAttr("style"); $("body").removeClass(container.group.options.bodyClass); console.log('Updating') init(e) } }) }
Working fragment :
function createNodes(e) { var foo = $('<li>Foo<ul></ul></li>'); var bar = $('<li>Bar<ul></ul></li>'); var baz = $('<li>Baz<ul></ul></li>'); bar.find('ul').append(baz); e.append(foo, bar); } function init(e) {
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//johnny.imtqy.com/jquery-sortable/js/jquery-sortable.js"></script> <div id="myroot"> </div>
source share