HTML ondragend Event Attribute
Description
The ondragend HTML event attribute fires when the user has finished dragging an element or text selection. It is one of the four drag-and-drop events that can be used to handle different stages of a drag-and-drop operation.
To use the ondragend event attribute, simply assign a JavaScript function to it. When the user finishes dragging the element, the function will be executed. The function will receive an event object as an argument, which contains information about the drag-and-drop event.
Here is an example of how to use the ondragend event attribute:
<div draggable="true" ondragend="myDragEndFunction()">
Drag me!
</div>
<script>
function myDragEndFunction(event) {
// Do something when the user finishes dragging the element
}
</script>
The ondragend event can be used for a variety of purposes, such as:
- Updating the UI when the user has finished dragging an element to a new location.
- Performing some action on the dragged element, such as saving it to a database or uploading it to a server.
- Preventing the user from dropping the element on certain targets.
Here are some examples of how the ondragend event can be used:
- A to-do list app could use the
ondragendevent to update the position of a to-do item when the user drags it to a new location in the list. - A file explorer app could use the
ondragendevent to upload a file to a server when the user drops it on the upload area. - A game could use the
ondragendevent to prevent the user from dropping a game piece on a certain location on the game board.
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 ondragend="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 ondragend 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
