Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ondragleave event

ondragleave event

Event Object Reference Event Object

Example

Execute JavaScript when the draggable element moves out of the drop target:

Try it »

The bottom of this article contains more examples.


Definition and Usage

The ondragleave event is triggered when the draggable element or selected text moves out of the drop target.

The ondragenter and ondragleave events can help users better understand the process of draggable elements entering and leaving the drop area. You can set different background colors when the draggable element enters and leaves the drop area.

Drag and drop is a very common feature in HTML5. For more information, see in our HTML tutorial HTML5 drag and drop.

Note: To make an element draggable, you need to use HTML5 draggable attribute.

Tip: Links and images are draggable by default and do not need the draggable attribute.

The following events are triggered during the drag and drop process:

- [ondragstart](event-ondragstart.html) - Triggered when the user starts dragging an element - [ondrag](event-ondrag.html) - Triggered while the element is being dragged - [ondragend](event-ondragend.html) - Triggered after the user finishes dragging the element - [ondragenter](event-ondragenter.html) - This event is triggered when the object dragged by the mouse enters its container range - [ondragover](event-ondragover.html) - This event is triggered when a dragged object is dragged within the container range of another object - ondragleave - This event is triggered when the object dragged by the mouse leaves its container range - [ondrop](event-ondrop.html) - This event is triggered when the mouse button is released during a drag operation

Browser support

The numbers in the table indicate the version number of the first browser to support the event.

Events
ondragleave 4.0 9.0 3.5 6.0 12.0

Syntax

In HTML:

<

element

ondragleave="

myScript

">

Try it

In JavaScript:

object

.ondragleave=function(){

myScript

};

Try it

In JavaScript, use the addEventListener() method:

object

.addEventListener("dragleave",

myScript

);

Try it

Note: Internet Explorer 8 and earlier IE versions do not support addEventListener() method.


Technical Details

Does it support bubbling: Yes
Is it cancelable: No
Event type: DragEvent
Supported HTML tags: All HTML elements

More examples

Example

The following example demonstrates all drag and drop events:

/* Triggered while dragging */

document.addEventListener("dragstart", function(event) {

    // The dataTransfer.setData() method sets the data type and the dragged data

    event.dataTransfer.setData("Text", event.target.id);

    // Output some text when dragging the p element

    document.getElementById("demo").innerHTML = "Start dragging the p element.";

    // Modify the opacity of the dragged element

    event.target.style.opacity = "0.4";

});

// While dragging the p element, change the color of the output text

document.addEventListener("drag", function(event) {

    document.getElementById("demo").style.color = "red";

});

// When the p element is dragged completely, output some text and reset the opacity

document.addEventListener("dragend", function(event) {

    document.getElementById("demo").innerHTML = "Finish dragging the p element";

    event.target.style.opacity = "1";

});

/* Triggered after dragging is completed */

// When the p element finishes dragging and enters the droptarget, change the border style of the div

document.addEventListener("dragenter", function(event) {

    if ( event.target.className == "droptarget" ) {

        event.target.style.border = "3px dotted red";

    }

});

// By default, data/elements cannot be dragged and dropped in other elements. For drop we must prevent the element's default handling

document.addEventListener("dragover", function(event) {

    event.preventDefault();

});

// When the draggable p element leaves the droptarget, reset the border style of the div

document.addEventListener("dragleave", function(event) {

    if ( event.target.className == "droptarget" ) {

        event.target.style.border = "";

    }

});

/* For drop, prevent the browser's default data handling (links are opened by default in drop)

Reset the color of the output text and the border color of the DIV

Use the dataTransfer.getData() method to obtain the drag and drop data

The id of the dragged data element ("drag1")

Append the dragged element to the drop element */

document.addEventListener("drop", function(event) {

    event.preventDefault();

    if ( event.target.className == "droptarget" ) {

        document.getElementById("demo").style.color = "";

        event.target.style.border = "";

        var data = event.dataTransfer.getData("Text");

        event.target.appendChild(document.getElementById(data));

    }

});

Try it »


Related pages

HTML Tutorial: HTML5 drag and drop

HTML Reference Manual: HTML draggable attribute

Event Object Reference Event Object