Skip to content

Buzzingz

Unleashing the Power of Code

  • HTML
  • CSS
  • JavaScript
  • Toggle search form

HTML Drag and Drop API

Posted on March 4, 2023April 1, 2023 By shani No Comments on HTML Drag and Drop API

The HTML Drag and Drop API is a set of events and interfaces that allow developers to enable drag and drop functionality in web applications. With this API, users can click and drag elements on a web page and drop them onto a target location.

To use the HTML Drag and Drop API, the following events can be used:

  1. ondragstart: This event is fired when the user starts to drag an element.
  2. ondrag: This event is fired continuously as the user is dragging an element.
  3. ondragend: This event is fired when the user finishes dragging an element.
  4. ondragenter: This event is fired when a draggable element enters a drop target.
  5. ondragover: This event is fired continuously as a draggable element is being dragged over a drop target.
  6. ondragleave: This event is fired when a draggable element leaves a drop target.
  7. ondrop: This event is fired when a draggable element is dropped onto a drop target.

To enable drag and drop functionality, the draggable attribute is added to the HTML element that can be dragged, and the ondragstart event is defined. The ondragover event is then used to allow the drop target to accept the draggable element, and the ondrop event is used to handle the drop action.

Here’s an example of how to use the HTML Drag and Drop API:

<!DOCTYPE html>
<html>
<head>
	<title>HTML Drag and Drop API</title>
	<style>
		#drag {
			width: 100px;
			height: 100px;
			background-color: red;
		}
		#drop {
			width: 200px;
			height: 200px;
			background-color: green;
			margin-top: 20px;
		}
	</style>
	<script>
		function dragStart(event) {
			event.dataTransfer.setData("text/plain", event.target.id);
		}
		function dragOver(event) {
			event.preventDefault();
		}
		function drop(event) {
			event.preventDefault();
			var data = event.dataTransfer.getData("text/plain");
			event.target.appendChild(document.getElementById(data));
		}
	</script>
</head>
<body>
	<div id="drag" draggable="true" ondragstart="dragStart(event)"></div>
	<div id="drop" ondragover="dragOver(event)" ondrop="drop(event)"></div>
</body>
</html>

In this example, the div element with the id “drag” can be dragged and dropped onto the div element with the id “drop”. The dragStart function is called when the drag operation starts and sets the data type and value to be transferred. The dragOver function is called continuously as the draggable element is being dragged over the drop target and prevents the default action of not allowing a drop. Finally, the drop function is called when the draggable element is dropped onto the drop target and appends the draggable element to the drop target.

HTML

Post navigation

Previous Post: HTML Geolocation API
Next Post: HTML Web Storage API

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

HTML Tutorial

  • HTML History
  • HTML Editors
  • What is an HTML Element?
  • HTML Attributes
  • HTML Headings
  • HTML Paragraphs
  • HTML Styles
  • HTML Text Formatting
  • HTML Quotation and Citation Elements
  • HTML Comments
  • HTML Colors
  • HTML Styles – CSS
  • HTML Links
  • HTML images
  • HTML Favicon
  • HTML Tables
    • HTML Table Colgroup
    • HTML Table Colspan and Rowspan
    • HTML Table Headers
    • HTML Table Padding and Spacing
    • HTML Table Sizes
    • HTML Table Styling
  • HTML List
  • HTML Ordered Lists
  • HTML Unordered List
  • HTML other lists
  • HTML Block and Inline Elements
  • HTML class Attribute
  • HTML id Attribute
  • HTML Iframes
  • HTML JavaScript
  • HTML File Paths
  • HTML – The Head Element
  • HTML Layout Elements and Techniques
  • HTML Responsive Web Design
  • HTML Computer Code Elements
  • HTML Semantic Elements
  • HTML Style Guide
  • HTML Entities
  • HTML Symbols
  • HTML Emojis
  • HTML Encoding (Character Sets)
  • HTML Uniform Resource Locators(URL)
  • HTML Versus XHTML
  • HTML Forms
  • HTML Form Attributes
  • HTML Form Elements
  • HTML Form Target Attribute
  • HTML Input Attributes
  • HTML Input form Attributes
  • HTML Input Types
  • HTML Canvas Graphics
  • HTML SVG Graphics
  • HTML Multimedia
  • HTML Video
  • HTML Audio
  • HTML Plug-ins
  • HTML YouTube Videos
  • HTML Geolocation API
  • HTML Drag and Drop API
  • HTML Web Storage API
  • HTML Web Workers API
  • HTML SSE API
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions

Copyright © 2023 Buzzingz.

Powered by PressBook WordPress theme