HTML ondragover Event Attribute
Description
The ondragover HTML event attribute fires when a draggable element or text selection is being dragged over a valid drop target. It is one of the four drag and drop events that occur on the drop target, along with ondragenter, ondragleave, and ondrop.
The ondragenter event fires when the dragged element first enters the drop target, the ondragover event fires repeatedly while the dragged element is over the drop target, the ondragleave event fires when the dragged element leaves the drop target, and the ondrop event fires when the dragged element is dropped on the drop target.
The ondragover event can be used to perform a variety of tasks, such as:
- Changing the appearance of the drop target to indicate that it is a valid drop target.
- Preventing the default drop behavior, such as preventing an image from being opened in a new browser window when it is dropped onto a link.
- Performing custom logic, such as validating the data being dragged before allowing it to be dropped.
To use the ondragover event, you simply need to add the attribute to the element that you want to be the drop target. The value of the attribute is a JavaScript function that will be executed when the ondragover event is fired.
Here is an example of how to use the ondragover event to prevent an image from being opened in a new browser window when it is dropped onto a link:
<a href="#" ondragover="event.preventDefault()">
<img src="image.png" draggable="true">
</a>
This code will prevent the image from being opened in a new browser window when it is dropped onto the link, even though the link has a href attribute.
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 ondragover="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 ondragover 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
