NextIndexPrevious

Programs in HTML5

9. Example for Drag and Drop.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
#board{
	border-style:groove;
	border-color:#039;
	width:300px;
	height:250px;
	float:left;
}
</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>
</head>

<body>
<div id="board" ondrop="drop(event)"
ondragover="allowDrop(event)"></div>

<div id="board" ondrop="drop(event)"
ondragover="allowDrop(event)"></div>

<img id="drag1" src="builder.jpg" draggable="true"
ondragstart="drag(event)">
</body>
</html>


Output :





Developed by


World Wide Web Consortium WHATWG

World Wide Web Consortium & WHATWG