HTML ondragstart Attribute

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

Definition and Usage

The ondragstart attribute is a global event handler that runs JavaScript when the dragstart event fires on the source — when the user begins dragging an element or text selection.

Use it to store payload with event.dataTransfer.setData(), set effectAllowed, or add a dragging style. Most elements need draggable="true". Drop targets still need ondragover + preventDefault() and ondrop. Reset visuals in ondragend.

Baseline · Widely available.

Not deprecated and not experimental. Stable since about July 2015. Per MDN, dragstart is cancelable and may bubble — preventDefault() can block the drag from starting.

Discouraged as inline HTML.

MDN discourages HTML event handler attributes. Prefer element.addEventListener("dragstart", handler) in production; use inline ondragstart for quick demos and learning.

HTML Syntax

syntax.html
<element ondragstart=" script">

script is JavaScript run when the drag begins. Prefer: element.addEventListener("dragstart", handler).

Attribute Values

The attribute value is JavaScript. Common patterns:

Value / patternDescriptionTry it
setData(type, data)Store text/plain (or other MIME) for the drop targetTry it
this.classList.add('dragging')Visual feedback while the drag is activeTry it
preventDefault()Cancel the drag so it does not startTry it
addEventListener("dragstart", …)Preferred modern APITry it

Live Preview

Drag the box. dragstart fades it and stores data; dragend restores opacity. Drop on the zone to read the payload.

Desktop mouse drag works best. Touch drag support varies by browser.

Source + drop target
Drag me
Drop zone

Waiting for dragstart…

HTML Examples

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

Example 1 — Fade Source on dragstart

Add a class when the drag begins; remove it in dragend.

HTML
<div id="item" draggable="true" ondragstart="startDrag(event)" ondragend="endDrag(event)">
  Drag me
</div>
<p id="msg"></p>

<script>
  function startDrag(e) {
    e.target.classList.add("dragging");
    document.getElementById("msg").textContent = "dragstart — dragging…";
  }

  function endDrag(e) {
    e.target.classList.remove("dragging");
    document.getElementById("msg").textContent = "dragend — finished.";
  }
</script>
Try It Yourself

How It Works

  1. draggable="true" lets a plain element start a drag.
  2. ondragstart runs once when the gesture begins.
  3. Add a class (opacity) so the source looks “picked up.”
  4. ondragend clears the class whether the drop succeeded or not.

Example 2 — setData for the Drop Target

Store text in dataTransfer during dragstart; read it in drop.

HTML
<div id="item" draggable="true">Card A</div>
<div id="zone">Drop here</div>
<p id="msg"></p>

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

  item.addEventListener("dragstart", function (e) {
    e.dataTransfer.setData("text/plain", "Card A");
    item.classList.add("dragging");
  });

  item.addEventListener("dragend", function () {
    item.classList.remove("dragging");
  });

  zone.addEventListener("dragover", function (e) {
    e.preventDefault();
  });

  zone.addEventListener("drop", function (e) {
    e.preventDefault();
    var data = e.dataTransfer.getData("text/plain");
    document.getElementById("msg").textContent = "Got: " + data;
  });
</script>
Try It Yourself

How It Works

  1. dragstart on the source calls setData("text/plain", …).
  2. dragover on the target calls preventDefault() so drop is allowed.
  3. drop reads the same type with getData("text/plain").
  4. Types must match — set and get the same MIME string.

Example 3 — Cancel dragstart Conditionally

dragstart is cancelable — block the drag when a condition fails.

HTML
<label>
  <input type="checkbox" id="lock"> Lock dragging
</label>
<div id="item" draggable="true">Drag me</div>
<p id="msg"></p>

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

  item.addEventListener("dragstart", function (e) {
    if (lock.checked) {
      e.preventDefault(); // cancel the drag
      document.getElementById("msg").textContent = "Drag blocked.";
      return;
    }
    document.getElementById("msg").textContent = "dragstart — allowed.";
  });
</script>
Try It Yourself

How It Works

  1. Check a condition inside dragstart (here: a checkbox).
  2. preventDefault() cancels the drag before it starts.
  3. No dragend / drop runs for a cancelled start.
  4. Useful for locked cards, disabled rows, or permission checks.

Edge Cases & Pitfalls

Most “dragstart never fires” bugs are a missing draggable="true".

source

Needs draggable="true"

Plain divs and custom cards will not start a user drag without it. Images/links may drag under the default auto behavior.

drop

setData ≠ drop alone

Storing data in dragstart does nothing unless the target allows drop via dragover + preventDefault and reads it in drop.

style

Reset in dragend

If you add a “dragging” class in dragstart, always remove it in dragend so cancelled drops do not leave a faded element.

a11y

Offer a non-drag path

Keyboard and many assistive tech users cannot drag. Provide buttons or menus that do the same move/copy action.

touch

Mobile varies

HTML Drag and Drop is mouse-oriented. Test touch devices before relying on it alone.

Browser Support

ondragstart is an HTML event handler attribute for the browser Drag and Drop API. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

ondragstart

Widely available since about July 2015. Desktop Chrome, Firefox, Safari, and Edge support it well. Touch drag UX still differs by device.

96% 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
Full support
Internet Explorer 9+
Supported
ondragstart Widely supported

Bottom line: Safe to use for starting drag payloads on modern sites. Prefer addEventListener, pair with draggable, and finish drops with dragover + drop.

Key Takeaways

  • Role: runs on the source when a drag begins — set data and visuals.
  • Needs draggable="true" on most elements.
  • setData in start; getData in drop; allow drop in dragover.
  • Cancelable: preventDefault() can block the drag from starting.

One line: ondragstart prepares the source payload — drop still needs dragover + drop.

Frequently Asked Questions

It is an inline event handler that runs JavaScript when the dragstart event fires on the source — when the user begins dragging an element or text selection.
Yes for most elements. Images and links are often draggable by default (auto). Plain divs and other nodes need draggable="true" for dragstart to fire from a user drag.
Store payload with event.dataTransfer.setData(), optionally set effectAllowed or a drag image, and add a visual class. Reset styles in dragend.
Yes. Calling preventDefault() in dragstart can cancel the drag so it does not start. Use that to block dragging under certain conditions.
Prefer element.addEventListener("dragstart", handler). Inline ondragstart works for learning. MDN discourages HTML event handler attributes in production.
ondragstart fires once when the drag begins on the source. ondragend fires when the drag finishes (success or cancel). Target events (dragenter, dragover, drop) fire on the drop zone.

Next: ondrop

Handle the payload when the user releases over a valid drop target.

ondrop 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