I am trying to dynamically create divs which will then be draggable. I found though, that the entire parent div moves along with it. If I create multiple child divs, they also move together with the parent. What am I doing wrong?
Here is my code:
$(function() {
$( "button#new-child" )
.button()
.click(function( event ) {
event.preventDefault();
$("#creation-box").append("<div class='draggable ui-widget-content'><p>Drag me around</p></div>").draggable();
});
});
and the html
<body>
<div id="toolbar">
<div class="header"> <h3>Toolbar</h3></div>
<button id="new-child">New Child</button>
</div>
<div id="creation-box">
<div class="header"><h3>Creation Box</h3></div>
</div>
</body>
Because you set the parent as draggable. Set your new element as draggable after creation only.
BTW,
.append()doesn’t have callback, so just add the.draggableafter element creation.