HTML ondrag Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

drag event

While moving.

03

draggable

Must be true.

04

vs dragstart

Start vs during.

05

addEventListener

Preferred way.

06

.ondrag

Property API.

Purpose of ondrag Attribute

The 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.

💡
Set draggable="true" on the source

Without draggable="true", most elements cannot be dragged and ondrag will never run. Production code should use element.addEventListener("drag", handler).

📝 Syntax

Set ondrag on a draggable element or assign element.ondrag:

ondrag.html
<script>

  function handleDrag(event) {

    document.getElementById("status").textContent = "Dragging…";

  }

</script>



<div draggable="true" ondrag="handleDrag(event)">Drag me</div>

<p id="status"></p>

Syntax Rules

  • Value is JavaScript code executed while the element is being dragged.
  • The element must be draggable — set draggable="true" on most elements.
  • drag fires on the source element being dragged, not on drop targets.
  • Event order: dragstart once, then many drag events, then dragend once.
  • JavaScript: element.ondrag = function(event) { … }.
  • Modern alternative: element.addEventListener("drag", handler).
  • The drag event bubbles — you can delegate on a parent container.

💎 Values

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.
  • JavaScript: box.ondrag = (e) => { … } assigns the handler.
ondrag-js.html
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";

};

⚡ Quick Reference

EventWhen it firesHandler
dragRepeatedly while source is draggedondrag
dragstartOnce when drag beginsondragstart
dragendOnce when drag endsondragend
dragoverOver a valid drop targetondragover
dropItem released on targetondrop
Bubbles?drag yesDelegate on parent

Applicable Elements

TargetSupported?Notes
<div>, <span>YesSet draggable="true"
<img>YesDraggable by default in many browsers
<a href>YesLinks are draggable by default
<li>YesCommon in sortable lists
<button>YesNeeds draggable="true"; add keyboard alternative

ondrag vs ondragstart vs ondragend

HandlerWhen it firesTypical use
ondragstartOnce at drag startSet dataTransfer, store drag payload
ondragRepeatedly while movingLive feedback, counters, opacity during drag
ondragendOnce when drag endsReset styles, finalize state

Examples Gallery

Inline ondrag on a draggable div, dynamic element.ondrag assignment, and comparing dragstart vs drag vs dragend on the same element.

👀 Live Preview

Drag the box to count how many drag events fire while moving:

Drag this box.

Waiting for a drag… (0 drag events)

Example — Inline ondrag on draggable div

Run a function while the user drags a draggable element:

inline-ondrag.html
<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>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

Assign the handler with element.ondrag or addEventListener:

dynamic-ondrag.html
<script>

  document.getElementById("dynamicDragElement").ondrag = function () {

    document.getElementById("log").textContent = "Dynamic drag handler ran.";

  };



  // Or preferred form:

  document.getElementById("dynamicDragElement").addEventListener("drag", handler);

</script>
Try It Yourself

How It Works

Register the listener once at page load. Each movement during a drag runs your handler on the source element.

Example — drag vs dragstart vs dragend

A full drag sequence fires dragstart once, many drag events, then dragend once:

drag-vs-dragstart.html
box.addEventListener("dragstart", () => {

  log("dragstart");

});



box.addEventListener("drag", () => {

  log("drag");

});



box.addEventListener("dragend", () => {

  log("dragend");

});
Try It Yourself

How It Works

Do not put heavy setup in ondrag — it runs continuously. Use ondragstart for initialization and ondragend for cleanup.

♿ Accessibility

  • Drag is pointer-heavy — Not all users can drag with a mouse. Provide keyboard or button alternatives for the same action.
  • Announce changes — Use aria-live regions when drag state changes so screen reader users get feedback.
  • Avoid alert() on drag — Inline status text or visible UI updates are more accessible than modal alerts.
  • Use semantic controls — For reordering lists, consider accessible patterns (move up/down buttons) alongside drag-and-drop.
  • Focus visible — Do not remove focus outlines on draggable controls; keyboard users need clear focus indicators.

🧠 How ondrag Works

1

User starts drag

dragstart fires once on source.

ondragstart
2

Pointer moves

drag fires repeatedly while moving.

ondrag
3

User releases

dragend fires once on source.

ondragend
=

Drop on target

dragover + drop on receiver.

Browser Support

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.

DOM Events · Fully supported

Universal ondrag support

All major browsers fire the underlying event and honor the ondrag handler attribute.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
ondrag attribute 99% supported

Bottom line: Universal support for draggable elements. Set draggable="true" and test drop targets with preventDefault() on dragover.

💡 Best Practices

✅ Do

  • Use addEventListener on the element — Clearer than inline ondrag.
  • Set draggable="true" — Required on most elements before drag events fire.
  • Keep drag handlers light — drag fires often; avoid heavy work on every fire.
  • Use dragstart for setup — Put dataTransfer and one-time logic in ondragstart, not ondrag.
  • Provide keyboard alternatives — Drag-and-drop alone is not accessible to all users.

❌ Don’t

  • Do not use alert() — Update visible status text instead of blocking modal dialogs.

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about ondrag

Bookmark these before wiring your next event handler.

5
Core concepts
🌐 02

draggable

Set true on source.

Required
🔄 03

vs dragstart

Start vs during.

Pattern
📝 04

addEventListener

Preferred.

Modern
05

Source only

Not drop target.

Note

❓ Frequently Asked Questions

It runs JavaScript when the drag event fires — repeatedly on the source element while it is being dragged, after dragstart.
Most elements with 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.
Prefer element.addEventListener("drag", …). Inline ondrag works for simple demos.
Yes, for most elements. Without draggable="true", the user cannot drag the element and ondrag will not run.
Yes. One listener on a parent can handle drag events from child draggable elements.

Handle drag events with JavaScript

Practice inline ondrag, addEventListener, and drag vs dragstart vs dragend in the Try It editor.

Try ondrag demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful