Hi all i have a function where i have to drag and drop elements here is my code:
$(document).ready(function() {
var i = 0;
$("button[id='columnadd']").click(function () {
alert(1);
var domElement = $('<aside id="shoppingCart' + i + '" class="shoppingCart"><h2 class="ui-widget-header">Add Menu Items Here</h2><aside class="ui-widget-content" id="droppable"><ol><li class="placeholder">Add your items here</li></ol></aside></aside>');
i++;
$(this).after(domElement);
});
$(".small_box li" ).draggable({
appendTo: "body",
helper: "clone"
});
});
$('#shoppingCart ol').droppable({
accept: '.small_box li',
drop: function (event, ui) {
alert(1);
$(this).find(".placeholder").remove();
$("<li></li>").text(ui.draggable.text()).appendTo(this);
}
});
here is my html:
<aside class="small_box">
<h4>BRANDS</h4>
<ul>
<li id ="brand1"><a class="" href="#">Brand1</a></li>
<li id ="brand2"><a href="#">Brand2</a></li>
<li id ="brand3"><a href="#">Brand3</a></li>
<li id ="brand4"><a href="#">Brand4</a></li>
</ul>
</aside>
i should be able to drop in at this place <li class="placeholder">Add your items here</li>
var domElement = $('<aside id="shoppingCart' + i + '" class="shoppingCart"><h2 class="ui-widget-header">Add Menu Items Here</h2><aside class="ui-widget-content" id="droppable"><ol><li class="placeholder">Add your items here</li></ol></aside></aside>');
any help is much appreciated
First, please fix your code indentations.
There are some mistakes on your code:
var i = 0;I think it should be a global variable. So put it outside your
$(document).ready(function() {})block;You create a DOM Element (which is the drop area) upon click and this element is not in your page until you click the trigger. By doing
$('#shoppingCart ol').droppable({...})in$(document).ready(function() {})block you try to adddroppableto an element that even not in your page, so this won’t do a thing.What you should do is make the element
droppableafter you create that element and put it on your page. here’s an example:and this is the function to make your dropped element has its own placeholder