ondragover = function(event) ); - Javascript DOM Event

Javascript examples for DOM Event:Element Event Attribute

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 {/*w  w  w  .j  a  v a2s  . c o m*/
    float: left;
    width: 200px;
    height: 35px;
    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.ondragstart = function(event) {
    event.dataTransfer.setData("Text", event.target.id);
};

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

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

drop.ondrop = 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