Horje
How to create HTML Drag and Drop Example

The example below is a simple drag and drop example:


Example of HTML Drag and Drop Example

The example below is a simple drag and drop example: Drag the Horje Profile image into the rectangle:
index.html
Example: HTML
<style>
#div1 {
  width: 350px;
  height: 70px;
  padding: 10px;
  border: 1px solid #aaaaaa;
}
</style>
<script>
function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
}
</script>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<img id="drag1" src="https://horje.com/avatar.png" draggable="true" ondragstart="drag(event)" width="336" height="69">

Output should be:

Example of HTML Drag and Drop Example





Category:
Web Tutorial
Sub Category:
HTML Drag and Drop API
Uploaded by:
Admin


Read Article
https://develop.horje.com/learn/1434/reference