HTML ondrag Event Attribute
Description
The ondrag HTML event attribute fires when an element or text selection is being dragged. It is one of several event attributes that are used in the HTML5 drag-and-drop API.
To use the ondrag attribute, you first need to set the draggable attribute on the element that you want to be draggable. Once you have done this, you can add the ondrag attribute to the element and specify a JavaScript function that will be executed when the element is being dragged.
The JavaScript function that you specify for the ondrag attribute will be passed an event object that contains information about the drag operation. This information includes the following:
- The current position of the pointer relative to the element being dragged.
- The offset of the pointer relative to the element being dragged when the drag operation started.
- The element being dragged.
You can use this information to perform various tasks, such as updating the position of the element being dragged, displaying a visual feedback to the user, or preventing the user from dragging the element to certain areas of the page.
Here is an example of how to use the ondrag attribute:
<div id="draggable" draggable="true" ondrag="myDragFunction(event)">
This element is draggable!
</div>
function myDragFunction(event) {
// Get the current position of the pointer relative to the element being dragged.
var pointerX = event.clientX;
var pointerY = event.clientY;
// Update the position of the element being dragged.
document.getElementById('draggable').style.left = pointerX + 'px';
document.getElementById('draggable').style.top = pointerY + 'px';
}
In this example, the myDragFunction() function will be executed every time the #draggable element is being dragged. The function will update the position of the #draggable element to the current position of the pointer.
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 ondrag="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 ondrag 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
