HTML5 supports native drag-and-drop. To make an element draggable, add the draggable="true" attribute and listen to events like dragstart, dragover, and drop.
HTML Drag and Drop API
Enable draggable elements and drop targets natively without third-party libraries.
Key Takeaways: HTML Drag and Drop API
Mastering HTML Drag and Drop API helps you write clean and reliable HTML code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
HTML Drag and Drop API Code Example
HTML<div id="target" style="width:150px;height:70px;border:2px dashed #999;padding:10px;">Drop here</div>
<div id="dragItem" draggable="true" style="padding:8px;background:#38bdf8;margin-top:10px;cursor:grab;">Drag me</div>
<script>
const item = document.getElementById("dragItem");
const target = document.getElementById("target");
item.ondragstart = (e) => e.dataTransfer.setData("text", e.target.id);
target.ondragover = (e) => e.preventDefault();
target.ondrop = (e) => {
e.preventDefault();
const id = e.dataTransfer.getData("text");
target.appendChild(document.getElementById(id));
};
</script>
Output
Element dragged and appended to drop zone
Free sign-in required · Google login available
Finished this lesson?
Mark it complete to track your progress.
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services