👀 Live Preview
Drag the box to count how many drag events fire while moving:
Waiting for a drag… (0 drag events)

The ondrag attribute is an inline event handler that runs JavaScript when the drag event fires. That happens repeatedly on the source element while the user is dragging it—after dragstart and before dragend. It is part of the HTML Drag and Drop API. The element must have draggable="true" (for most elements) so the browser allows dragging. Use ondrag to track movement, update a ghost image, or show live feedback during a drag. The event bubbles. For production sites, prefer addEventListener("drag", …) over inline handlers. Pair drag sources with drop targets using ondragover and ondrop on the receiving element.
Inline JS.
While moving.
Must be true.
Start vs during.
Preferred way.
Property API.
ondrag AttributeThe primary purpose of ondrag is to define what happens while a draggable element is being moved. Common uses include counting drag events, updating a status label, changing opacity of the source, or syncing a custom drag preview. It connects continuous pointer movement during a drag to JavaScript behavior without a full page reload.
Do not confuse ondrag with ondragstart or ondragend. dragstart fires once when dragging begins; drag fires many times during movement; dragend fires once when the user releases. Use ondragstart to set drag data and ondrag for live feedback during the move. Avoid alert() in handlers—update visible status text instead.
Without draggable="true", most elements cannot be dragged and ondrag will never run. Production code should use element.addEventListener("drag", handler).
Set ondrag on a draggable element or assign element.ondrag:
<script>
function handleDrag(event) {
document.getElementById("status").textContent = "Dragging…";
}
</script>
<div draggable="true" ondrag="handleDrag(event)">Drag me</div>
<p id="status"></p>draggable="true" on most elements.drag fires on the source element being dragged, not on drop targets.dragstart once, then many drag events, then dragend once.element.ondrag = function(event) { … }.element.addEventListener("drag", handler).drag event bubbles — you can delegate on a parent container.The ondrag attribute accepts a string of JavaScript code:
ondrag="handleDrag(event)" — Call a named function with the event object.ondrag="this.style.opacity='0.5'" — Inline statement on the element.box.ondrag = (e) => { … } assigns the handler.const box = document.getElementById("dragBox");
box.draggable = true;
box.addEventListener("drag", () => {
document.getElementById("status").textContent = "Still dragging…";
});
document.getElementById("dragBox").ondrag = function (event) {
event.target.style.opacity = "0.6";
};| Event | When it fires | Handler |
|---|---|---|
drag | Repeatedly while source is dragged | ondrag |
dragstart | Once when drag begins | ondragstart |
dragend | Once when drag ends | ondragend |
dragover | Over a valid drop target | ondragover |
drop | Item released on target | ondrop |
| Bubbles? | drag yes | Delegate on parent |
| Target | Supported? | Notes |
|---|---|---|
<div>, <span> | Yes | Set draggable="true" |
<img> | Yes | Draggable by default in many browsers |
<a href> | Yes | Links are draggable by default |
<li> | Yes | Common in sortable lists |
<button> | Yes | Needs draggable="true"; add keyboard alternative |
ondrag vs ondragstart vs ondragend| Handler | When it fires | Typical use |
|---|---|---|
ondragstart | Once at drag start | Set dataTransfer, store drag payload |
ondrag | Repeatedly while moving | Live feedback, counters, opacity during drag |
ondragend | Once when drag ends | Reset styles, finalize state |
Inline ondrag on a draggable div, dynamic element.ondrag assignment, and comparing dragstart vs drag vs dragend on the same element.
Drag the box to count how many drag events fire while moving:
Waiting for a drag… (0 drag events)
Run a function while the user drags a draggable element:
<div draggable="true" ondrag="handleDrag(event)">Drag me</div>
<p id="msg"></p>
<script>
var dragCount = 0;
function handleDrag(event) {
dragCount += 1;
document.getElementById("msg").textContent =
"drag event #" + dragCount;
}
</script>The browser fires drag on the source element each time the pointer moves during a drag. The inline ondrag attribute calls your function on every fire.
Assign the handler with element.ondrag or addEventListener:
<script>
document.getElementById("dynamicDragElement").ondrag = function () {
document.getElementById("log").textContent = "Dynamic drag handler ran.";
};
// Or preferred form:
document.getElementById("dynamicDragElement").addEventListener("drag", handler);
</script>Register the listener once at page load. Each movement during a drag runs your handler on the source element.
A full drag sequence fires dragstart once, many drag events, then dragend once:
box.addEventListener("dragstart", () => {
log("dragstart");
});
box.addEventListener("drag", () => {
log("drag");
});
box.addEventListener("dragend", () => {
log("dragend");
});Do not put heavy setup in ondrag — it runs continuously. Use ondragstart for initialization and ondragend for cleanup.
aria-live regions when drag state changes so screen reader users get feedback.dragstart fires once on source.
drag fires repeatedly while moving.
dragend fires once on source.
dragover + drop on receiver.
The drag event and ondrag handler are part of the HTML Drag and Drop API and supported in all modern browsers — Chrome, Firefox, Safari, and Edge. Touch drag behavior may differ; test on your target devices.
ondrag supportAll major browsers fire the underlying event and honor the ondrag handler attribute.
Bottom line: Universal support for draggable elements. Set draggable="true" and test drop targets with preventDefault() on dragover.
ondrag.drag fires often; avoid heavy work on every fire.dataTransfer and one-time logic in ondragstart, not ondrag.The ondrag attribute runs JavaScript while a draggable element is being moved and the drag event fires repeatedly on the source. Use it for live feedback during a drag — not for one-time setup or final cleanup.
Prefer addEventListener("drag", …), set draggable="true", pair with ondragstart and ondragend, and offer keyboard alternatives for accessibility.
ondragBookmark these before wiring your next event handler.
drag fires often.
EventSet true on source.
RequiredStart vs during.
PatternPreferred.
ModernNot drop target.
Notedrag event fires — repeatedly on the source element while it is being dragged, after dragstart.draggable="true": div, span, li, img, and more. The event fires on the element being dragged.ondragstart fires once when dragging begins. ondrag fires many times while the pointer moves during the drag.element.addEventListener("drag", …). Inline ondrag works for simple demos.draggable="true", the user cannot drag the element and ondrag will not run.Practice inline ondrag, addEventListener, and drag vs dragstart vs dragend in the Try It editor.
5 people found this page helpful