The ondrag attribute is an event-handler attribute for the element being dragged. It runs JavaScript when the drag event fires — repeatedly while the user drags that element (or a text selection).
dragstart — drag begins (set dataTransfer here in real apps).
drag — continues while the pointer gesture is active (many times).
dragend — drag finished; clean up styles or state.
This demo logs the first drag only so the list stays readable.
Edge Cases & Pitfalls
Most surprises are missing draggable, assuming a fixed fire rate, or forgetting drop-target events.
setup
Need draggable="true"
Most elements will not drag without it. Images and links are often exceptions.
timing
No fixed interval
Browsers fire drag on their own schedule. Do not rely on one event per pixel or a constant ms rate.
drop
ondrag is not the drop
To allow a drop, the target must handle dragover (usually with preventDefault) and drop.
a11y
Offer a non-drag path
Keyboard and many touch users cannot use HTML DnD reliably. Provide buttons or another move UI.
perf
Keep drag handlers light
Heavy DOM work on every drag can stutter. Prefer updating only when something meaningful changes.
Compatibility
Browser Support
ondrag is an HTML event-handler attribute for the Drag and Drop API in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
ondrag
Widely available since about July 2015 on desktop. Mobile/touch HTML drag-and-drop remains inconsistent — test your targets and provide fallbacks.
94%Global support (approx.)
Google Chrome4+
Full support
Mozilla Firefox3.5+
Full support
Apple Safari3.1+
Full support
Microsoft Edge12+
Full support
Opera12+
Full support
Samsung InternetModern
Partial / check
Internet Explorer9+
Supported
ondragWidely supported (desktop)
Bottom line: Safe for desktop drag demos and progressive enhancement. Prefer addEventListener, keep drag handlers light, and always offer a non-drag alternative.
Remember
Key Takeaways
Role: run script repeatedly while the source is being dragged.
Setup: usually needs draggable="true".
Sequence:dragstart → drag… → dragend.
A11y: always provide a non-drag way to move items.
One line:ondrag is the “still dragging” pulse — enable with draggable, keep it light, finish with dragend.
Frequently Asked Questions
It is an event-handler attribute on the element being dragged. While the user drags that element (or a text selection), the drag event fires repeatedly and the JavaScript in ondrag runs.
Set the HTML draggable attribute to "true" (for example <div draggable="true">). Some elements like images and links are often draggable by default.
ondragstart fires once when the drag begins. ondrag fires repeatedly during the drag. ondragend fires once when the drag finishes (drop or cancel).
It depends on the browser, OS, and pointer movement. Do not assume a fixed interval or one event per pixel. Treat it as a periodic “still dragging” signal.
Both work. For learning, ondrag in HTML is fine. In production code, prefer element.addEventListener("drag", handler).
No. The drag event and ondrag handler are part of the HTML Drag and Drop API and are Baseline widely available since about July 2015. Touch support is still inconsistent — offer a non-drag alternative.