HTML | DOM ondragend Event

The HTML DOM ondragend event occurs when a draggable element finishes dragging.

There are some events that are used and occurred in the different stages of a drag and drop operation:

    Events fired on the draggable target:

  • ondragstart: occurs when dragging of an element started.
  • ondrag: occurs while an element is dragging.
  • ondragend: occurs when dragging of an element finished.
    Events fired on the drop target:

  • ondragenter: occurs when the dragged element entered into the drop target.
  • ondragover: occurs when the dragged element is over the drop target.
  • ondragleave: occurs when the dragged element leaves from the drop target.
  • ondrop: occurs when the dragged element is dropped on the drop target.

Syntax:
In HTML:

<element ondragend="myScript">

In JavaScript:

object.ondragend = function(){myScript};

In JavaScript, using the addEventListener() method:

object.addEventListener("dragend", myScript);

Example: Using HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        HTML DOM ondrop Event
    </title>
    <style>
        .droptarget {
            float: center;
            width: 300px;
            height: 50px;
            margin: 20px;
            padding: 5px;
            border: 6px solid green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1 style="color:green">
        GeeksforGeeks
    </h1>
        <h2>HTML DOM ondrop Event</h2>
  
        <div class="droptarget" 
             ondrop="Eledrop(event)" 
             ondragover="EleallowDrop(event)">
            
            <p ondragstart="EledragStart(event)" 
               ondragend="EledragEnd(event)" 
               draggable="true" 
               id="dragtarget">
              Draggable element
          </p>
        </div>
  
        <div class="droptarget" 
             ondrop="Eledrop(event)"
             ondragover="EleallowDrop(event)">
      </div>
  
        <p id="demo"></p>
  
        <script>
            function EledragStart(event) {
                event.dataTransfer.setData("Text", event.target.id);
                document.getElementById(
                  "demo").innerHTML = "Dragging Started";
            }
  
            function EledragEnd(event) {
                document.getElementById(
                  "demo").innerHTML = "Dragging Finished";
            }
  
            function EleallowDrop(event) {
                event.preventDefault();
            }
  
            function Eledrop(event) {
                event.preventDefault();
                var data = event.dataTransfer.getData("Text");
                event.target.appendChild(document.getElementById(data));
            }
        </script>
  </center>
</body>
  
</html>

chevron_right


Output

Example: Using JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        HTML DOM ondrop Event
    </title>
    <style>
        .droptarget {
            float: center;
            width: 300px;
            height: 50px;
            margin: 20px;
            padding: 5px;
            border: 6px solid green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1 style="color:green">
        GeeksforGeeks
    </h1>
        <h2>HTML DOM ondrop Event</h2>
  
        <div class="droptarget">
            <p draggable="true"
               id="dragtarget">
              Draggable Element
          </p>
        </div>
  
        <div class="droptarget"></div>
  
        <p id="demo"></p>
  
        <script>
            // Events fired on the drag target 
            document.ondragstart = function(event) {
                event.dataTransfer.setData("Text", event.target.id);
                document.getElementById(
                  "demo").innerHTML = "Dragging Started";
            };
  
            document.ondragend = function(event) {
                document.getElementById(
                  "demo").innerHTML = "Dragging Finished";
            };
  
            // Events fired on the drop target 
            document.ondragover = function(event) {
                event.preventDefault();
            };
  
            document.ondrop = function(event) {
                event.preventDefault();
                if (event.target.className == "droptarget") {
                    var data = event.dataTransfer.getData("Text");
                    event.target.appendChild(document.getElementById(data));
                }
            };
        </script>
    </center>
</body>
  
</html>

chevron_right


Output

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        HTML DOM ondrop Event
    </title>
    <style>
        .droptarget {
            float: center;
            width: 300px;
            height: 50px;
            margin: 20px;
            padding: 5px;
            border: 6px solid green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1 style="color:green">
        GeeksforGeeks
    </h1>
        <h2>HTML DOM ondrop Event</h2>
        <div class="droptarget">
            <p draggable="true"
               id="dragtarget">
              Draggable element
          </p>
        </div>
  
        <div class="droptarget"></div>
  
        <p id="demo"></p>
  
        <script>
            // Events fired on the drag target 
            document.addEventListener("dragstart", function(event) {
                event.dataTransfer.setData("Text", event.target.id);
                document.getElementById(
                  "demo").innerHTML = "Dragging Started";
            });
  
            document.addEventListener("dragend", function(event) {
                document.getElementById(
                  "demo").innerHTML = "Dragging Finished";
            });
  
            // Events fired on the drop target 
            document.addEventListener("dragover", function(event) {
                event.preventDefault();
            });
  
            document.addEventListener("drop", function(event) {
                event.preventDefault();
                if (event.target.className == "droptarget") {
                    var data = event.dataTransfer.getData("Text");
                    event.target.appendChild(document.getElementById(data));
                }
            });
        </script>
    </center>
</body>
  
</html>

chevron_right


Output

Supported Browsers: The browsers supported by HTML DOM ondragend Event are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.