
- 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
How to set whether an element is draggable or not in HTML?
Use the draggable attribute to set whether the element is draggable or not. Set it using the values true and false.
Example
You can try to run the following code to implement draggable element and drag an element −
<!DOCTYPE HTML> <html> <head> <style> .drag { float: left; width: 100px; height: 35px; border: 2px dashed #876587; margin: 15px; padding: 10px; } </style> </head> <body> <div class="drag" ondrop = "drop(event)" ondragover = "dropNow(event)"> <p ondragstart = "dragStart(event)" ondrag = "draggingNow(event)" draggable = "true" id = "dragtarget">Drag!</p> </div> <div class = "drag" ondrop="drop(event)" ondragover = "dropNow(event)"></div> <div id = "box"> </div> <p>Drag the left box to the right or drag the right box to the left.</p> <script> function dragStart(event) { event.dataTransfer.setData("Text", event.target.id); } function draggingNow(event) { document.getElementById("box").innerHTML = "Dragged successfully!"; } function dropNow(event) { event.preventDefault(); } function drop(event) { event.preventDefault(); var data = event.dataTransfer.getData("Text"); event.target.appendChild(document.getElementById(data)); document.getElementById("box").innerHTML = "The element dropped successfully!"; } </script> </body> </html>
- Related Articles
- How to set whether or not an element is resizable by the user with JavaScript?
- Check whether the content of an element is editable or not in HTML
- How to specify whether the content of an element should be translated or not in HTML?
- How to set whether the text of an element can be selected or not with JavaScript?
- Set whether or not an element should be visible while not facing the screen with JavaScript?
- How to specify whether the element is to have its spelling and grammar checked or not in HTML?
- How to create a draggable HTML element with JavaScript and CSS?
- Preventing an image from being draggable or selectable in HTML without using JS
- How to specify that an element is not yet relevant in HTML?
- How to set whether an element should be visible in JavaScript?
- HTML draggable Attribute
- How to check in R whether a matrix element is present in another matrix or not?
- Set whether the text of the element can be selected or not with CSS
- How to set whether the dragged data is copied, moved, or linked, when dropped in HTML?
- How to check an element exist or not in jQuery?

Advertisements