
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML ondrag Event Attribute
The HTML ondrag event attribute is triggered when the user drags an HTML element in an HTML document. The HTML image and link elements are dragged by default so there is no need to specify ondrag attribute on them.
Syntax
Following is the syntax −
<tagname ondrag=”script”></tagname>
Example
Let us see an example of HTML ondrag event Attribute −
<!DOCTYPE html> <html> <head> <style> body { color: #000; height: 100vh; background-color: #FBAB7E; background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%); text-align: center; } .drop-target { display: inline-block; width: 150px; height: 150px; border: 2px solid #FFF; margin: 1rem; vertical-align: middle; padding: 20px; } .circle { background: #db133a; height: 40px; width: 40px; border-radius: 50%; } .show { color: #fff; font-size: 1.2rem; } </style> </head> <body> <h1>HTML ondrag Event Attribute Demo</h1> <div class="drop-target" ondrop="drop(event)" ondragover="allowDrop(event)"> <p ondragstart="dragStart(event)" ondrag="dragging(event)" draggable="true" class="circle"></p> </div> <div class="drop-target" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <p>Now try to drag and drop the red circle</p> <div class="show"></div> <script> function dragStart(event) { event.dataTransfer.setData("Text", event.target.id); } function dragging(event) { document.querySelector(".show").innerHTML = "Circle is being dragged"; } function allowDrop(event) { event.preventDefault(); } function drop(event) { event.preventDefault(); event.target.appendChild(document.querySelector('.circle')); document.querySelector(".show").innerHTML = "Circle was dropped"; } </script> </body> </html>
Output
Now try to drag and drop the red circle between the two boxes to observe how ondrag event attribute works.
- Related Articles
- HTML onblur Event Attribute
- HTML onclick Event Attribute
- HTML onchange Event Attribute
- HTML oncopy Event Attribute
- HTML oncut Event Attribute
- HTML oncontextmenu Event Attribute
- HTML onfocus Event Attribute
- HTML oninput Event Attribute
- HTML oninvalid Event Attribute
- HTML onpaste Event Attribute
- HTML onresize Event Attribute
- HTML onreset Event Attribute
- HTML onsubmit Event Attribute
- HTML onscroll Event Attribute
- HTML onsearch Event Attribute

Advertisements