Live Demo
Drag the green box into the dashed drop zone. The zone highlights while you hover over it, then turns solid and confirms the drop.
Drag me
Drop here
jQuery UI's .droppable() interaction turns an element into a valid drop target for .draggable() elements. Together they give you drag-and-drop features like "drag items into a shopping cart" or "drag a card into a bin" without writing any low-level mouse-tracking code yourself.
$('#source').draggable();
$('#target').droppable({
drop: function(event, ui){
// ui.draggable is a jQuery object - the element that was dropped
$(this).text('Dropped: ' + ui.draggable.text());
}
});
accept
$('#cart').droppable({
accept: '.fruit', // only draggables with class "fruit" are accepted
drop: function(event, ui){ ... }
});
tolerance
$('#target').droppable({
tolerance: 'pointer' // 'fit' | 'intersect' (default) | 'pointer' | 'touch'
});
| Tolerance | Meaning |
|---|---|
fit | Draggable must be fully inside the droppable to accept. |
intersect | Default - at least 50% of the draggable overlaps the droppable. |
pointer | The mouse pointer must be over the droppable. |
touch | Any part of the draggable touches the droppable. |
over / out
$('#target').droppable({
over: function(){ $(this).addClass('ui-state-highlight'); },
out: function(){ $(this).removeClass('ui-state-highlight'); }
});
Drag the green box into the dashed drop zone. The zone highlights while you hover over it, then turns solid and confirms the drop.
draggable="true", dragstart/dragover/drop events) covered earlier in the HTML tutorial - jQuery UI's version works with plain mouse/touch events instead, so it behaves consistently across older browsers too.