HTML ondragenter Attribute

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

Introduction

The ondragenter attribute is an inline event handler that runs JavaScript when the dragenter event fires on a drop target. That happens when a dragged item’s pointer crosses into the target element’s boundary—including when moving over nested descendants. It is part of the HTML Drag and Drop API: pair a draggable="true" source with a receiver that listens for dragenter, dragover, dragleave, and drop. Use ondragenter mainly for visual feedback (highlighting the zone). To allow a drop, call event.preventDefault() in dragover—not because dragenter always requires it. For production sites, prefer addEventListener("dragenter", …) over inline handlers.

What You’ll Learn

01

Event handler

Inline JS.

02

dragenter event

On drop target.

03

Highlight zone

Visual feedback.

04

vs dragover

Enter vs hover.

05

addEventListener

Preferred way.

06

Counter pattern

Nested children.

Purpose of ondragenter Attribute

The primary purpose of ondragenter is to define what happens when a dragged item enters a drop target. Common uses include adding a highlight class, updating a status label (“Release to drop”), or playing a subtle animation so users know the zone is active. It connects pointer movement over a receiver to JavaScript feedback without a full page reload.

Do not confuse ondragenter with ondragstart or ondragend—those fire on the source being dragged. dragenter fires on the target when the pointer crosses its boundary. dragover fires repeatedly while hovering and is where preventDefault() enables dropping. dragleave fires when leaving the boundary; pair it with dragenter to remove highlights. Avoid alert() in handlers; update visible status text instead.

💡
preventDefault on dragover, not dragenter

Browsers block drops unless the target calls event.preventDefault() during dragover. dragenter is for highlighting—do not assume it must always call preventDefault(). Production code should use element.addEventListener("dragenter", handler).

📝 Syntax

Set ondragenter on a drop target or assign element.ondragenter:

ondragenter.html
<script>

  function handleDragEnter(event) {

    document.getElementById("dropZone").classList.add("over");

    document.getElementById("status").textContent = "dragenter — pointer entered zone.";

  }

</script>



<div draggable="true">Drag me</div>

<div id="dropZone" ondragenter="handleDragEnter(event)">Drop zone</div>

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

Syntax Rules

  • Value is JavaScript code executed when the pointer enters the drop target boundary.
  • dragenter fires on the drop target, not on the draggable source.
  • Pair a draggable="true" source with handlers on the receiver.
  • Call event.preventDefault() in dragover on the target to allow drop.
  • dragenter can fire multiple times when moving over nested children—use a counter or relatedTarget checks.
  • JavaScript: element.ondragenter = function(event) { … }.
  • Modern alternative: element.addEventListener("dragenter", handler).

💎 Values

The ondragenter attribute accepts a string of JavaScript code:

  • ondragenter="handleDragEnter(event)" — Call a named function with the event object.
  • ondragenter="this.classList.add('over')" — Inline statement to highlight the target.
  • JavaScript: zone.ondragenter = (e) => { … } assigns the handler.
  • Pair with ondragleave or a counter to remove highlight when the pointer exits.
ondragenter-js.html
const zone = document.getElementById("dropZone");

let enterCount = 0;



zone.addEventListener("dragenter", (event) => {

  enterCount += 1;

  zone.classList.add("over");

  document.getElementById("status").textContent =

    "dragenter — entered drop zone.";

});



zone.addEventListener("dragleave", (event) => {

  enterCount -= 1;

  if (enterCount === 0) zone.classList.remove("over");

});



zone.addEventListener("dragover", (event) => {

  event.preventDefault();

});



document.getElementById("dropZone").ondragenter = function (event) {

  console.log("Pointer entered target.");

};

⚡ Quick Reference

EventWhen it firesHandler
dragenterPointer enters target boundaryondragenter
dragoverRepeatedly while over targetondragover
dragleavePointer leaves target boundaryondragleave
dropItem released on valid targetondrop
preventDefaultRequired in dragover to allow dropNot required in dragenter for highlighting
Nested childrendragenter/dragleave may fire repeatedlyUse enter counter pattern

Applicable Elements

TargetSupported?Notes
<div>, <section>YesCommon drop zones; no draggable needed on target
<ul>, <li>YesSortable lists; watch nested dragenter events
<td>, <th>YesTable cell drop targets
<article>, <main>YesLarge layout drop areas
Draggable sourceSeparateSet draggable="true" on source; use ondragstart there

ondragenter vs ondragover vs ondragleave vs ondrop

HandlerWhen it firesElementTypical use
ondragenterBoundary entered (once per crossing)Drop targetHighlight zone, show “drop here” status
ondragoverRepeatedly while hoveringDrop targetpreventDefault() to allow drop; continuous feedback
ondragleaveBoundary leftDrop targetRemove highlight; pair with dragenter
ondropOnce when released on valid targetDrop targetAccept payload, move DOM node, update data

Examples Gallery

Inline ondragenter highlighting a drop zone, dynamic element.ondragenter assignment, and comparing dragenter vs dragover vs dragleave on a target.

👀 Live Preview

Drag the box into the drop zone. dragenter highlights the target:

Drag this box.
Drop zone

Waiting for dragenter…

Example — Inline ondragenter on drop zone

Highlight the target when a draggable item enters its boundary:

inline-ondragenter.html
<div draggable="true">Drag me</div>

<div id="dropZone" ondragenter="handleDragEnter(event)">Drop zone</div>

<p id="msg"></p>



<script>

  function handleDragEnter(event) {

    document.getElementById("dropZone").classList.add("over");

    document.getElementById("msg").textContent = "dragenter — entered drop zone.";

  }

</script>
Try It Yourself

How It Works

The browser fires dragenter on the drop target when the dragged item crosses its boundary. The inline ondragenter attribute calls your highlight function.

Dynamic Values with JavaScript

Assign the handler with element.ondragenter or addEventListener:

dynamic-ondragenter.html
<script>

  document.getElementById("dynamicDropTarget").ondragenter = function (event) {

    document.getElementById("log").textContent =

      "Dynamic dragenter — pointer entered target.";

    event.currentTarget.classList.add("over");

  };



  // Or preferred form:

  document.getElementById("dynamicDropTarget").addEventListener("dragenter", handler);

</script>
Try It Yourself

How It Works

Register the listener once at page load. When the pointer enters the drop target during a drag, your handler runs on the receiver element.

Example — dragenter vs dragover vs dragleave

On a drop target, dragenter and dragleave fire on boundary changes; dragover fires continuously while hovering:

dragenter-vs-dragover.html
target.addEventListener("dragenter", () => { log("dragenter (boundary entered)"); });

target.addEventListener("dragover", (e) => {

  e.preventDefault();

  log("dragover (while hovering)");

});

target.addEventListener("dragleave", () => { log("dragleave (boundary left)"); });

target.addEventListener("drop", () => { log("drop (released on target)"); });
Try It Yourself

How It Works

Put highlight logic in ondragenter and cleanup in ondragleave on the target. Put preventDefault() in dragover and receive logic in ondrop.

♿ Accessibility

  • Drag is pointer-heavy — Not all users can drag with a mouse. Provide keyboard or button alternatives for the same action.
  • Announce zone state — Use aria-live regions when dragenter highlights a zone so screen reader users get feedback.
  • Avoid alert() on dragenter — Inline status text or visible UI updates are more accessible than modal alerts.
  • Do not rely on dragenter alone — Finalize important actions in accessible controls, not only after a drag gesture.
  • Visible focus — Ensure highlighted drop zones have sufficient color contrast and do not hide focus outlines on interactive elements.

🧠 How ondragenter Works

1

User drags source

dragstart fires on draggable source.

ondragstart
2

Pointer enters target

dragenter fires on drop target boundary.

ondragenter
3

Pointer hovers

dragover fires repeatedly; call preventDefault.

ondragover
=

Optional drop

drop on release; dragleave when exiting boundary.

Browser Support

The dragenter event and ondragenter 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 ondragenter support

All major browsers fire the underlying event and honor the ondragenter 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
ondragenter attribute 99% supported

Bottom line: Universal support for dragenter on drop targets. Pair with dragover + preventDefault() and a draggable source.

💡 Best Practices

✅ Do

  • Use addEventListener on the target — Clearer than inline ondragenter.
  • preventDefault in dragover — Required on the drop target to allow dropping; not the main job of dragenter.
  • Highlight in dragenter — Add classes or border styles when the pointer enters the zone.
  • Pair dragleave or use a counter — Remove highlight reliably when nested children cause repeated dragenter events.
  • Pair with ondrop on targets — dragenter shows feedback; drop handles the payload.

❌ Don’t

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

Conclusion

The ondragenter attribute runs JavaScript when a dragged item enters a drop target and the dragenter event fires on the receiver. Use it for highlighting and status feedback—not as a substitute for dragover + preventDefault() when you need to allow drops.

Prefer addEventListener("dragenter", …), pair with ondragleave or a counter pattern, call preventDefault() in dragover, and offer keyboard alternatives for accessibility.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about ondragenter

Bookmark these before wiring your next event handler.

5
Core concepts
🌐 02

Boundary enter

Pointer crosses in.

Behavior
🔄 03

Highlight zone

Visual feedback.

Use case
📝 04

addEventListener

Preferred.

Modern
05

vs dragover

Enter vs hover.

Compare

❓ Frequently Asked Questions

It runs JavaScript when the dragenter event fires on a drop target—when a dragged item’s pointer enters that element’s boundary.
No. Call event.preventDefault() in dragover on the drop target to allow dropping. dragenter is mainly for visual feedback and highlighting.
ondragenter fires when the pointer crosses into the target boundary. ondragover fires repeatedly while hovering and is where preventDefault() enables drop.
Prefer element.addEventListener("dragenter", …). Inline ondragenter works for simple demos.
Moving over nested child elements can trigger dragenter and dragleave on the parent repeatedly. Use an enter counter or check event.relatedTarget with dragleave to keep highlight state stable.
No. draggable="true" goes on the source being dragged. The drop target only needs dragenter, dragover, dragleave, and drop handlers.

Highlight drop zones with JavaScript

Practice inline ondragenter, addEventListener, dragenter vs dragover, and the counter pattern in the Try It editor.

Try ondragenter 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