Search code examples
jquerydraggablelive

Jquery Draggable / Live


I have:

$(document).ready(function () {

      $(".div1, .div2, .div3, .div4, .div5").draggable();


      $("#menu").click(function () {

           $("<div class='div1'></div>").appendTo("#layout");

      });

});

But the draggable function works just to the divs that already exist when the page is loaded. When I add dynamically, I could not drag them.

My Html:

     <div id="layout">
       <div class="div1"></div>
     </div>

Solution

  • You just need to add the draggable handler to your new div with :

     $("<div class='div1'></div>").appendTo("#layout").draggable();
    

    for example.

    Here's a live example.