Curriculum
Course: HTML Basic
Login

Curriculum

HTML Basic

HTML Introduction

0/1

HTML Editors

0/1

HTML Attributes

0/1

HTML Paragraphs

0/1

HTML Formatting

0/1

HTML Comments

0/1

HTML Favicon

0/1

HTML Page Title

0/1

HTML Iframes

0/1

HTML Java Script

0/1

HTML File Paths

0/1

HTML Symbols

0/1
Text lesson

Make an Element Draggable

Firstly, to enable an element to be draggable, set the draggable attribute to true:

<img draggable=“true”>

What to Drag – ondragstart and setData()

Next, define what action should occur when the element is being dragged.

In the example above, the ondragstart attribute invokes a function, drag(event), which determines the data to be dragged.

Using dataTransfer.setData() method establishes both the data type and the value of the data being dragged.

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

In this instance, the data type is set to “text” and the value is the id of the draggable element (“drag1”).