HTML ondragleave Event Attribute
Description
The ondragleave HTML event attribute is fired when a draggable element or text selection leaves a valid drop target. It is one of several events that can occur during a drag and drop operation, along with ondragstart, ondrag, ondragend, ondragenter, ondragover, and ondrop.
The ondragleave event can be used to provide feedback to the user about the drag and drop operation. For example, you could change the appearance of the drop target when the dragged element enters or leaves it. You could also use the event to perform other actions, such as disabling the drop target or preventing the drop operation from occurring.
To use the ondragleave event, you simply need to add the attribute to the HTML element that you want to be the drop target. The value of the attribute should be a JavaScript function that will be called when the event occurs.
Here is an example of how to use the ondragleave event:
<div id="drop-target" ondragleave="myFunction()">
Drop your files here
</div>
function myFunction() {
// Change the appearance of the drop target
document.getElementById('drop-target').style.backgroundColor = 'white';
}
In this example, the myFunction() function will be called when the dragged element leaves the drop target. The function simply changes the background color of the drop target back to white.
In HTML5, seven event attributes were added that are used at various stages of the drag and drop operation:
- Events that occur with the drag object:
- ondragstart (triggered at the beginning of an item drag operation).
- ondrag (triggered when an item is dragged).
- ondragend (triggered when the user has finished dragging and dropping the item).
- Events that occur with the object being dragged onto:
- ondragenter (when the item will be transferred to the specified zone (target for transfer)).
- ondragover (triggered when an element is moved over a valid transfer zone).
- ondragleave (triggered when an element leaves an acceptable zone for transfer).
- ondrop (triggered after the dragged item descends on the drag object).
Syntax
<element ondragleave="script">
Values
- scriptThe name of the script to use when the event has been triggered.
Example
Browser Support
The following information will show you the current browser support for the HTML ondragleave event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.
This event attribute is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 13th October 2023
