JavaScript ondragend Event

You are Here:

JavaScript ondragend Event

The ondragend event occurs when the user has finished dragging a draggable element.

ondragend 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(){ alert("You finished dragging"); } </script> </body> </html>

Syntax

As Attribute

<element ondragend = "JavaScript">

As Property

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

Using Event Listener

object.addEventListener("dragend" , myScript);

ondragend 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" ondrag="onDrag()" ondragstart="onDragStart(event)" 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(){ alert("You finished dragging"); } myImg.ondragend = onDragEnd; </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" ondrag="onDrag()" ondragstart="onDragStart(event)" 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(){ alert("You finished dragging"); } myImg.addEventListener("dragend", onDragEnd); </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