HTML ondrop Event Attribute
Description
The ondrop HTML event attribute fires when a draggable element or text selection is dropped on a valid drop target. Drag and drop is a common feature in HTML5 that allows users to move elements around a web page or between web pages.
To make an element draggable, you can use the draggable attribute. Links and images are draggable by default.
The ondrop event attribute takes a JavaScript function as its value. This function will be executed when the draggable element or text selection is dropped on the drop target.
Here is an example of how to use the ondrop event attribute:
<div id="drop-target" ondrop="myFunction(event)">
Drop your element here!
</div>
<script>
function myFunction(event) {
// Get the element that was dropped.
var droppedElement = event.target;
// Get the drop target.
var dropTarget = event.currentTarget;
// Do something with the dropped element and the drop target.
// For example, you could move the dropped element to the drop target.
}
</script>
The ondrop event attribute is a powerful tool that can be used to create interactive and user-friendly web applications.
Here are some examples of how the ondrop event attribute can be used:
- To allow users to upload files by dragging and dropping them onto a web page.
- To create a drag-and-drop sorting feature.
- To create a drag-and-drop image gallery.
- To create a drag-and-drop form builder.
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 ondrop="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 ondrop 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
