Reputation: 6259
I'm experimenting with JQuery ui and the draggable, droppable ...
So I tried to make some divs draggable and when they are dragged on droppable elements the dragged element should be appended before the droppable element! So I have this elementary html:
<div class="container">
<div class="box" id="1">1</div>
<div class="box" id="2">2</div>
<div class="box" id="3">3</div>
</div>
<div class="container">
....
And my Code looks like this:
$('.box').wrap("<div class='wrapper'></div>");
$('.box').before("<div class='drop'></div>");
$('.wrapper').draggable();
$('.drop').droppable({
drop: handleDrop
});
function handleDrop(event,ui){
var draggable = ui.draggable;
alert( 'The square with ID "' + draggable.attr('id') + '" was dropped onto me!' );
draggable.insertBefore($(this));
};
So when an element is dropped onto a droppable element the function handleDrop
is triggered. How you can see at the end of the code i try to insert the draggable element before the droppable element:
draggable.insertBefore($(this));
But somehow this won't work it only produces a white gap:
Why?
Upvotes: 1
Views: 574
Reputation: 16116
Assuming you just wanted to append the box
element with an ID:
$('.box').wrap("<div class='wrapper'></div>");
$('.box').before("<div class='drop'></div>");
$('.wrapper').draggable();
$('.drop').droppable({
drop: handleDrop
});
function handleDrop(event,ui){
var draggable = ui.draggable;
alert( 'The square with ID "' + draggable.find('.box').attr('id') + '" was dropped onto me!' );
draggable.find('.box').insertBefore($(this));
};
Update
If you want to append both the .box
and droppable
:
function handleDrop(event,ui){
var draggable = ui.draggable;
alert(ui.draggable.html());
alert( 'The square with ID "' + draggable.find('.box').attr('id') + '" was dropped onto me!' );
draggable.children().insertBefore($(this));
};
Upvotes: 1