HTML Drag & Drop API

In HTML, any element can be dragged and dropped.

In HTML, any element can be dragged and dropped.

 

Drag and drop

Drag and drop is a very popular feature. It's when you "grab" an object and drag it to another location. Users can select draggable elements with the mouse, drag those elements onto the draggable element, and release them by releasing the mouse button. A transparent image of the draggable elements follows the cursor during the drag process.

Browser support

The numbers in the table indicate the first browser version to fully support drag-and-drop functionality.

API
Drag and Drop
Google Chrome MS Edge Firefox Safari Opera
4.0 9.0 3.5 6.0 12.0

Examples of drag and drop in HTML.

Here's an example of simple drag and drop:

 

It might seem complicated, but consider all the different parts of a drag-and-drop event.

Create a draggable element.

First, to make an element draggable, set the property draggableto true:

 

 

Specify what data is being pulled - ondragstart and setData()

Next, specify what will happen when the element is dragged.

In the example above, the property ondragstartcalls the function drag(event), specifying which data will be pulled.

The method dataTransfer.setData()sets the data type and value of the data being pulled:

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

In this case, the data type is "text" and the value is the ID of the draggable element ("drag1").

Where to drop it - ondragover

The event ondragoverspecifies where the dragged data can be dropped.

By default, it's not possible to drop data or elements into other elements. To enable dropping, the element's default handling must be disabled.

This is done by calling the method event.preventDefault()for the event ondragover:

event.preventDefault()

Perform an ondrop.

When data is dragged and dropped, a drop event occurs.

In the example above, the property ondropcalls the function,   drop(event):

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

The code is explained as follows:

  • Call this PreventDefault()to prevent the browser from processing the data by default (the default is to open it as a link when dropped).
  • Retrieve the data pulled using the method dataTransfer.getData(). This method will return any data set to the same type as in the method.setData()
  • The data being dragged is the ID of the element being dragged ("drag1").
  • Connect the dragged element to the dropped element.
Related posts
  • HTML SSE API

    server-sent events (sse) allow a website to receive updates from a server.
  • HTML Web Workers API

    a web worker is a javascript script that runs in the background, without affecting the page's performance.
  • HTML Web Storage API

    web hosting is more secure, and large amounts of data can be stored locally without impacting website performance.
Other HTML articles
  • HTML Geolocation API

    html geolocation api được sử dụng để xác định vị trí của người dùng.
  • YouTube video in HTML

    cách dễ nhất để phát video bằng html là sử dụng youtube.
  • Inserting video in HTML5

    cách chèn video vào html như thế nào? nếu đang muốn tìm hiểu cách chèn video vào website html, mời bạn tham khảo bài viết dưới đây.
  • What is HTML? Basic HTML structure

    html là một phần không thể thiếu khi nói đến lập trình web. mức độ phổ biến của html ngày càng tăng và nó được coi là tiêu chuẩn web chính thức. vậy cụ thể html là gì? html ra đời như thế nào và cấu trúc của html ra sao? hãy cùng tìm hiểu ngay sau đây nhé.
  • How to Learn HTML Language

    html là chữ viết tắt của hyper text markup language (ngôn ngữ đánh dấu siêu văn bản), và là loại mã hay ngôn ngữ được sử dụng để tạo nên các trang web. có thể hơi nản lòng một chút nếu bạn chưa từng viết mã bao giờ trước đây, nhưng tất cả những gì bạn cần để thử làm là một chương trình biên tập văn bản đơn giản và một trình duyệt internet. bạn thậm chí có thể nhận ra vài html được sử dụng để thay đổi kiểu chữ trên các diễn đàn trực tuyến, trong các hồ sơ trực tuyến được tùy biến, hoặc trong các bài viết của tipsmake. html là một công cụ hữu ích cho bất cứ ai sử dụng internet, việc học những thứ căn bản về html có thể mất ít thời gian hơn là bạn nghĩ.
  • How to create a simple contact form using HTML, CSS, and JavaScript

    học cách tạo một biểu mẫu liên hệ cho web thật dễ dàng theo các bước sau, đảm bảo bạn giao tiếp hiệu quả với khách truy cập.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup