addEventListener("dragover", function(event)); - Javascript DOM Event

Javascript examples for DOM Event:addEventListener

Description

The ondragover event occurs when a draggable element or text selection is being dragged over a valid drop target.

Summary

Bubbles Yes
Cancelable Yes
Supported HTML tags: All HTML elements

Demo Code

ResultView the demo in separate window

<!DOCTYPE HTML>
<html>
<head>
<style>
#droptarget {/*from   www  . j a  v  a  2s. c om*/
    float: left;
    width: 200px;
    height: 45px;
    margin: 55px;
    margin-top: 150px;
    padding: 10px;
    border: 1px solid red;
}
</style>
</head>
<body>

<p draggable="true" id="dragtarget">Drag me into the rectangle!</p>

<div id="droptarget"></div>

<p id="demo"></p>

<script>
var drag = document.getElementById("dragtarget");
var drop = document.getElementById("droptarget");

drag.addEventListener("dragstart", function(event) {
    event.dataTransfer.setData("Text", event.target.id);
});

drag.addEventListener("drag", function(event) {
    document.getElementById("demo").innerHTML = "The p element is being dragged.";
});

drop.addEventListener("dragover", function(event) {
    event.preventDefault();
    document.getElementById("demo").innerHTML = "The p element is OVER the droptarget.";
    event.target.style.border = "4px dotted purple";
});

drop.addEventListener("drop", function(event) {
    event.preventDefault();
    var data = event.dataTransfer.getData("Text");
    event.target.appendChild(document.getElementById(data));
    document.getElementById("demo").innerHTML = "The p element was dropped.";
});
</script>

</body>
</html>

Related Tutorials