HTML ondragenter Event Attribute
Description
The ondragenter HTML event attribute fires when a draggable element or text selection enters a valid drop target. It is one of several events that can be used to implement drag-and-drop functionality in web pages.
To use the ondragenter event attribute, you must first set the draggable attribute to true on the element that you want to make draggable. You can then add an ondragenter event listener to the drop target element. This event listener will be executed whenever a draggable element enters the drop target.
The ondragenter event object provides a number of properties that can be used to get information about the drag-and-drop operation, such as the element that is being dragged and the drop target. You can use this information to implement custom behavior for your drag-and-drop operation.
For example, you could use the ondragenter event to change the appearance of the drop target element when a draggable element is over it. You could also use the ondragenter event to prevent certain elements from being dropped into the drop target.
Here is an example of how to use the ondragenter event attribute to change the background color of a drop target element when a draggable element is over it:
<div id="drop-target" ondragenter="this.style.backgroundColor = 'red';">
Drop the image here
</div>
Here is an example of how to use the ondragenter event attribute to prevent certain elements from being dropped into a drop target element:
<div id="drop-target" ondragenter="if (event.target.classList.contains('no-drop')) { event.preventDefault(); }">
Drop the image here
</div>
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 ondragenter="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 ondragenter 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
