- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Code Builder
- 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
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - Global draggable Attribute
The draggable is an HTML global attribute that is used to specify whether an element is draggable or not.
If this attribute is not set, its value is auto, which means drag behavior is the default browser behavior. Only links and images are by default draggable.
For other elements, we have to use the draggable attribute, and the draggable attribute is often used in the drag and drop operation.
This attribute can have the following values −
true − the element can be dragged.
false − the element cannot be dragged.
Syntax
Following is the syntax of the draggable attribute −
<element draggable = "true | false" >
Example
In the example below, we're creating an HTML document and using the draggable attribute in order to help the JavaScript function in dragging and dropping the paragraph element, as follows−
<!DOCTYPE html> <html> <head> <style> #div { width: 300px; height: 50px; padding: 12px; border: 2px solid gray; } </style> </head> <body> <div id="div" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <br> <p id="drag" draggable="true" ondragstart="drag(event)">This is a draggable paragraph. Drag this element into the rectangle box.</p> <script> function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("Text", ev.target.id); } function drop(ev) { let data = ev.dataTransfer.getData("Text"); ev.target.appendChild(document.getElementById(data)); ev.preventDefault(); } </script> </body> </html>
When we execute the above script, it will generate an outptyu consisting of the text along with a div box on the webpage. when the user tries to drag the text and drop in the div box the eent gets triggered.
Example
Considering the another example, Where we are going to use the draggable attribute
<!DOCTYPE html> <html> <head> <style> #div { width: 350px; height: 70px; padding: 12px; border: 4px solid green; } h2 { color: green; border: 4px solid green; background-color: purble; margin: 20px; } span { color: black; } </style> </head> <body> <div id="div" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <br> <h2 id="drag" draggable="true" ondragstart="drag(event)">tutorials <span>point</span> </h2> <p draggable="true">Easy to learn!</p> <script> function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("Text", ev.target.id); } function drop(ev) { let data = ev.dataTransfer.getData("Text"); ev.target.appendChild(document.getElementById(data)); ev.preventDefault(); } </script> </body> </html>
On executing the above script, the output window will pop up displaying the text along with a div box on the webpage. when the user tries to drag and drop in the div box the event gets triggered.
To Continue Learning Please Login
Login with Google