JavaScript ondrag Event

You are Here:

JavaScript ondrag Event

The ondrag event occurs when a draggable element is being dragged.

ondrag Event as Attribute

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point">Drag the ball inside the Box:</p> <div id="myDiv" ondrop="onDrop(event)" ondragover="onDragOver(event)"></div> <img id="myImg" src="/football.svg" draggable="true" ondrag="onDrag()" ondragstart="onDragStart(event)" ondragend="onDragEnd()"> <script> var x = document.getElementById("point"); function onDragOver(e) { e.preventDefault(); } function onDragStart(e) { e.dataTransfer.setData("text", e.target.id); } function onDrop(e) { var data = e.dataTransfer.getData("text"); e.target.appendChild(document.getElementById(data)); x.innerHTML = "Good Job"; } function onDrag(){ x.innerHTML = "You are dragging..."; } function onDragEnd(){ if(x.innerText != 'Good Job') x.innerHTML = "Poor Job, Try Again"; } </script> </body> </html>

Syntax

As Attribute

<element ondrag = "JavaScript">

As Property

object.ondrag = function(){ // code };

Using Event Listener

object.addEventListener("drag" , myScript);

ondrag Event as Property

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point">Drag the ball inside the Box:</p> <div id="myDiv" ondrop="onDrop(event)" ondragover="onDragOver(event)"></div> <img id="myImg" src="/football.svg" draggable="true" ondragstart="onDragStart(event)" ondragend="onDragEnd()" width="95" height="95"> <script> var x = document.getElementById("point"); var myImg = document.getElementById("myImg"); function onDragOver(e) { e.preventDefault(); } function onDragStart(e) { e.dataTransfer.setData("text", e.target.id); } function onDrop(e) { var data = e.dataTransfer.getData("text"); e.target.appendChild(document.getElementById(data)); x.innerHTML = "Good Job"; } function onDrag(){ x.innerHTML = "You are dragging..."; } function onDragEnd(){ if(x.innerText != 'Good Job') x.innerHTML = "Poor Job, Try Again"; } myImg.ondrag = onDrag; </script> </body> </html>

Using addEventListener() Method

Note: The addEventListener() method is not supported in Internet Explorer 8 and below.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point">Drag the ball inside the Box:</p> <div id="myDiv" ondrop="onDrop(event)" ondragover="onDragOver(event)"></div> <img id="myImg" src="/football.svg" draggable="true" ondragstart="onDragStart(event)" ondragend="onDragEnd()" width="95" height="95"> <script> var x = document.getElementById("point"); var myImg = document.getElementById("myImg"); function onDragOver(e) { e.preventDefault(); } function onDragStart(e) { e.dataTransfer.setData("text", e.target.id); } function onDrop(e) { var data = e.dataTransfer.getData("text"); e.target.appendChild(document.getElementById(data)); x.innerHTML = "Good Job"; } function onDrag(){ x.innerHTML = "You are dragging..."; } function onDragEnd(){ if(x.innerText != 'Good Job') x.innerHTML = "Poor Job, Try Again"; } myImg.addEventListener("drag", onDrag); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author