HTML ondragover Attribute

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

Definition and Usage

The ondragover attribute is a global event handler that runs JavaScript when the dragover event fires on a drop target — repeatedly while a dragged item or text selection is over that element.

Its most important job: call event.preventDefault() so the browser allows a drop. Pair a draggable="true" source with ondragenter / ondragleave for highlight, and keep heavy work out of dragover (it fires often).

Baseline · Widely available.

Not deprecated and not experimental. Stable since about July 2015. Per MDN, dragover is cancelable and may bubble. Firing rate is browser/OS dependent (spec hint ≈ 350 ms ±200 ms).

Discouraged as inline HTML.

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

HTML Syntax

syntax.html
<element ondragover=" script">

script is JavaScript run on each dragover. Prefer: element.addEventListener("dragover", handler).

Attribute Values

The attribute value is JavaScript. Common patterns:

Value / patternDescriptionTry it
event.preventDefault()Required on the target to allow a later dropTry it
dropEffectSet event.dataTransfer.dropEffect to copy, move, link, or noneTry it
ondragover="…"Inline handler string (fine for demos)Try it
addEventListener("dragover", …)Preferred modern APITry it

Live Preview

Toggle preventDefault on dragover, then drag the box onto the zone and release. Without it, drop does not fire.

dragover

Desktop mouse drag works best. Watch the status when you release over the zone.

Drop target
Drag me
Drop zone

preventDefault on — drop should work.

HTML Examples

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

Example 1 — preventDefault Allows Drop

Call preventDefault() in dragover, then handle drop.

HTML
<div id="item" draggable="true">Drag me</div>
<div id="zone" ondragover="allowDrop(event)" ondrop="handleDrop(event)">
  Drop here
</div>
<p id="msg"></p>

<script>
  function allowDrop(e) {
    e.preventDefault(); // without this, drop will not fire
  }

  function handleDrop(e) {
    e.preventDefault();
    document.getElementById("msg").textContent = "drop — item released.";
    e.currentTarget.appendChild(document.getElementById("item"));
  }
</script>
Try It Yourself

How It Works

  1. Source is draggable="true".
  2. dragover on the target calls preventDefault() — this marks the zone as a valid drop.
  3. drop then fires when the user releases.
  4. Skip preventDefault and the item “snaps back”; drop never runs.

Example 2 — dropEffect Cursor Hint

Set dataTransfer.dropEffect during dragover to hint copy vs move.

HTML
<div draggable="true">Drag me</div>
<div id="zone">Drop (copy effect)</div>
<p id="msg"></p>

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

  zone.addEventListener("dragover", function (e) {
    e.preventDefault();
    e.dataTransfer.dropEffect = "copy"; // cursor hint
  });

  zone.addEventListener("drop", function (e) {
    e.preventDefault();
    document.getElementById("msg").textContent =
      "Dropped with dropEffect hint: copy";
  });
</script>
Try It Yourself

How It Works

  1. preventDefault still required so drop is allowed.
  2. dropEffect values: copy, move, link, none.
  3. Cursor feedback depends on the OS and browser — treat it as a hint.
  4. Actual move/copy logic still belongs in your drop handler.

Example 3 — dragover vs dragenter Frequency

dragenter fires on boundary entry; dragover keeps firing while hovering.

HTML
<div draggable="true">Drag me</div>
<div id="zone">Drop target</div>
<p id="msg"></p>

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

  zone.addEventListener("dragenter", function () {
    overCount = 0;
    document.getElementById("msg").textContent = "dragenter — entered.";
  });

  zone.addEventListener("dragover", function (e) {
    e.preventDefault();
    overCount += 1;
    document.getElementById("msg").textContent =
      "dragover — " + overCount + " times while hovering";
  });
</script>
Try It Yourself

How It Works

  1. dragenter — once (per boundary crossing) when entering.
  2. dragover — many times while the pointer stays over the target.
  3. Keep it light — avoid expensive work in dragover.
  4. Always call preventDefault() here if you want drop.

Edge Cases & Pitfalls

Most “drop does nothing” bugs are a missing preventDefault in dragover.

drop

No preventDefault → no drop

Browsers block drops by default. Call preventDefault() on every dragover for that target (and usually again in drop).

perf

Fires repeatedly

Do not run heavy DOM or network work here. Highlight in dragenter/dragleave; keep dragover tiny.

compare

Not the same as dragenter

dragenter is for “just entered.” dragover is continuous hover + allow-drop signal.

a11y

Offer a non-drag path

Pointer drag is hard for keyboard users. Provide buttons that do the same move/copy action.

touch

Mobile varies

HTML DnD is historically mouse-oriented. Test touch devices before relying on it alone.

Browser Support

ondragover 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

ondragover

Widely available since about July 2015. Desktop Chrome, Firefox, Safari, and Edge support it well. Event frequency and dropEffect cursors still differ by platform.

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
ondragover Widely supported

Bottom line: Safe to use as the allow-drop gate. Prefer addEventListener, always preventDefault when drop should work, and keep the handler light.

Key Takeaways

  • Role: fires while hovering a drop target — call preventDefault() to allow drop.
  • vs enter: enter once at boundary; over repeatedly while hovering.
  • dropEffect: optional cursor hint (copy / move / link / none).
  • Keep it light: put highlight in enter/leave; put payload work in drop.

One line: ondragover + preventDefault() unlocks drop — without it, nothing lands.

Frequently Asked Questions

It is an inline event handler that runs JavaScript when the dragover event fires on a drop target — repeatedly while a dragged item or text selection is over that element.
By default the browser does not allow dropping on most elements. Call event.preventDefault() in every dragover handler on the target so the drop event can fire.
ondragenter fires when the pointer crosses into the target. ondragover fires repeatedly while hovering and is where preventDefault() enables drop.
It depends on the browser, OS, and pointer movement. The HTML specification suggests roughly 350 ms (±200 ms). Do not assume a fixed interval or one event per pixel.
Prefer element.addEventListener("dragover", handler). Inline ondragover works for learning. MDN discourages HTML event handler attributes in production.
On dragover you can set event.dataTransfer.dropEffect to "copy", "move", "link", or "none" to hint the cursor feedback for the potential drop.

Next: ondragstart

Set drag data when the user begins dragging the source element.

ondragstart tutorial →

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