jquery - How to drop elements to a dynamically generated DIV, Drag function is working Fine -
here jquery code i'm using:
jquery(function() { jquery(".component").draggable({ // use helper-clone append 'body' not 'contained' pane helper: function() { return jquery(this).clone().appendto('body').css({ 'zindex': 5 }); }, cursor: 'move', containment: "document" }); jquery('.drag-drop-box').droppable({ accept: '.component', drop: function(event, ui) { if (!ui.draggable.hasclass("dropped")) jquery(this).append(jquery(ui.draggable).clone().addclass("dropped").draggable()); } }); });
this code works fine on pre-loaded div. creating divs dynamically how can working dynamically generated div.
this html code f static code dropping elements working well
<div class="item-box"> <div id="tabs-1"> <div class="drag-drop-box"> </div> </div> </div>
and here jquery code that's creating divs dynamically:
function addtab() { var label = tabtitle.val() || "tab " + tabcounter, id = "tabs-" + tabcounter, li = $( tabtemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label) ), tabcontenthtml = tabcontent.val();// || "tab " + tabcounter + " content."; tabs.find( ".ui-tabs-nav" ).append( li ); tabs.append( "<div id='" + id + "'><div class='drag-drop-box'></div></div>" ); tabs.tabs( "refresh" ); tabcounter++; }
i think need make dynamically created div droppable.
to add code @ end of addtab() function
jquery('#'+id).droppable({ activeclass: 'drag-drop-box-hover', accept: '.component', drop: function(event, ui) { if (!ui.draggable.hasclass("dropped")) jquery(this).append(jquery(ui.draggable).clone().addclass("dropped").draggable()); } });
i guess current jquery code runs once when page finished loading. @ time existing divs made droppable. new ones code needs run again.
Comments
Post a Comment