HTML 5 how to drag and drop

Let us add Drag and Drop event  using HTML5.

  • Drag and drop is a very common feature. It is when you “grab” an object and drag it to a different location.
  • In HTML5, drag and drop is part of the standard, and any element can be draggable.

Below is the code for html file.

<!DOCTYPE HTML>
<html>
<head>

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));
}

<link href=”scro.css” rel=”stylesheet” type=”text/css”>
</head>
<body>

<br>
<img id=”drag1″ src=”dre.png” draggable=”true” ondragstart=”drag(event)” width=”356″ height=”89″>

</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s