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
Consider an example in which the <p> element can be moved between two <div> blocks. To do this, we will set four event attributes on the <div> blocks and three on the moveable <p> paragraph.
When we start moving the <p> element, two events are triggered: the first is triggered at the beginning
of the operation of dragging the element with the mouse (ondragstart), on which we installed a script that
allows you to store data and its type (setData method), the second ondrag event fires at the moment the
drag started and informs us of this in the <p id="info"> </p> element.
By default, items cannot be placed in other items. To make this accessible (canceling the default browser) we use the preventDefault() method, which is triggered when an element is moved over a valid zone for transfer (the ondragover event attribute)
ondragenter event occurs when the element is moved to the specified zone, and a script is triggered that sets the background color to yellow and the dashed border to black. When an element leaves a given zone, the border and background style values are returned to their original form (ondragleave event)
The example uses two more event attributes: the first is ondrop, which is triggered when an element
descends onto a drag object, a script is launched that allows you to receive data (getData method) and the
second attribute of events is ondragend, which is triggered when the user has finished dragging and
dropping the element and runs a script that informs us about the completion of the drag and drop procedure
in the <p id="info"> </p> element.
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.
Desktop





Tablets & Mobile





