HTML ondrag Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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

Pair it with draggable="true". Typical order: dragstart (once) → drag (many times) → dragend (once). Drop targets use ondragover and ondrop.

Baseline · Widely available.

Not deprecated and not experimental. The drag event is stable across modern browsers since about July 2015. It is cancelable and may bubble.

Touch & keyboard caveat.

HTML drag-and-drop is mainly pointer-based. Touch support varies, and keyboard users need another way to move items. Prefer addEventListener in apps.

HTML Syntax

syntax.html
<element draggable="true" ondrag=" script">

script is JavaScript that runs during the drag. Equivalent JS: el.ondrag = handler or el.addEventListener("drag", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run repeatedly while draggingTry it
ondrag="fn()"Call a named function from the attributeTry it
+ draggable="true"Required on most elements so a drag can startTry it
addEventListener("drag")Preferred modern listener on the drag sourceTry it
With start / endLog dragstart → drag → dragend sequenceTry it

Live Preview

Drag the box. drag increments a counter while you hold and move (or while the platform keeps firing updates).

Action

Requires a mouse or trackpad in most browsers. Release to fire dragend.

Draggable source

Drag me

drag events: 0

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Inline ondrag Counter

Count drag firings while the user moves a draggable element.

HTML
<p id="item" draggable="true" ondrag="onDragging()">Drag me</p>
<p id="status">drag events: 0</p>

<script>
  var count = 0;
  function onDragging() {
    count += 1;
    document.getElementById("status").textContent = "drag events: " + count;
  }
</script>
Try It Yourself

How It Works

  1. draggable="true" lets the paragraph start a drag.
  2. User presses and moves — the browser starts a drag operation.
  3. drag fires repeatedly and ondrag runs onDragging().
  4. Counter updates — useful for demos; avoid heavy work on every drag.

Example 2 — addEventListener on the Source

Preferred script pattern: update status while dragging.

HTML
<p id="item" draggable="true">Drag me</p>
<p id="status">Idle</p>

<script>
  var item = document.getElementById("item");
  var status = document.getElementById("status");

  item.addEventListener("drag", function () {
    status.textContent = "Dragging…";
  });
  item.addEventListener("dragend", function () {
    status.textContent = "Drag ended";
  });
</script>
Try It Yourself

How It Works

  1. Select the source with getElementById.
  2. Listen for drag to react while the gesture continues.
  3. Listen for dragend to reset UI when the user releases.
  4. Keep handlers light — drag can fire often.

Example 3 — dragstart → drag → dragend

Log the usual source-side sequence for one drag gesture.

HTML
<p id="item" draggable="true">Drag me</p>
<ul id="log"></ul>

<script>
  var item = document.getElementById("item");
  var log = document.getElementById("log");
  var dragLogged = false;

  function add(line) {
    var li = document.createElement("li");
    li.textContent = line;
    log.appendChild(li);
  }

  item.addEventListener("dragstart", function () {
    dragLogged = false;
    add("dragstart");
  });
  item.addEventListener("drag", function () {
    if (!dragLogged) {
      add("drag (first of many)");
      dragLogged = true;
    }
  });
  item.addEventListener("dragend", function () {
    add("dragend");
  });
</script>
Try It Yourself

How It Works

  1. dragstart — drag begins (set dataTransfer here in real apps).
  2. drag — continues while the pointer gesture is active (many times).
  3. dragend — drag finished; clean up styles or state.
  4. 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.

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 Chrome 4+
Full support
Mozilla Firefox 3.5+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 12+
Full support
Samsung Internet Modern
Partial / check
Internet Explorer 9+
Supported
ondrag Widely 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.

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.

Next: ondragend

Run JavaScript when a drag operation finishes.

ondragend tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful