Drag and Drop
<!DOCTYPE HTML>
<html>
<head>
<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>
</head>
<body>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<img id="drag1" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69">
</body>
</html> It is when you "grab" an object and drag it to a different location.
In HTML5, drag and drop is part of the standard: any element can be draggable.
Make an Element Draggable
What to Drag - ondragstart and setData()
Where to Drop - ondragover
Do the Drop - ondrop
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
//Code explained:
//-Call preventDefault() to prevent the browser default handling of the data
// (default is open as link on drop)
//-Get the dragged data with the dataTransfer.getData() method. This method
// will return any data that was set to the same type in the setData() method
//-The dragged data is the id of the dragged element ("drag1")
//-Append the dragged element into the drop element
Semantic portal