codegurukool.com · Free coding tutorials & online compiler

Home › HTML Tutorial › Drag and drop API

Drag and drop API

Move elements with the mouse.

Set draggable, then handle dragstart, dragover (call preventDefault) and drop.

Example code

<style>#z{height:80px;border:2px dashed #888;margin-top:10px}#d{width:60px;background:gold;padding:6px}</style>
<div id='d' draggable='true'>Drag me</div><div id='z'>Drop here</div>
<script>
d.ondragstart=e=>e.dataTransfer.setData('t','d');
z.ondragover=e=>e.preventDefault();
z.ondrop=e=>{e.preventDefault();z.appendChild(d)};
</script>'

Run this code in the online editor

← Geolocation APIPopover API →