Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

ondragover — handling dragging over an element

The ondragover attribute contains JavaScript that handles dragover events while a drag is over a target. Canceling the event with event.preventDefault() allows an ordinary element to participate in accepting a drop.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

ondragover="event.preventDefault()"

ondragover is the event handler content attribute for dragover. Its value is JavaScript code, not a Boolean. The handler receives access to event; the event fires repeatedly while dragging over a target, not just when entering it.

For an ordinary drop target, call event.preventDefault() when handling dragover. Canceling the event signals acceptance of the operation; setting event.dataTransfer.dropEffect alone is insufficient. The dropEffect value, such as copy, must be compatible with the source's effectAllowed. Some editable elements have default text-drop behavior.

ondragover does not automatically read or insert dragged content. Read data with getData() in the drop handler; during dragover, the data store is in protected mode. In the example, dragstart stores text, dragenter and dragover cancel their events, and drop updates the result using textContent.

Distinguish the attribute from the JavaScript property element.ondragover, which takes a function, and from addEventListener('dragover', handler), which registers a listener using the event name without the on prefix.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>ondragover example</title>
<p draggable="true"
   ondragstart="event.dataTransfer.setData('text/plain', 'HELLO'); event.dataTransfer.effectAllowed = 'copy'">Drag HELLO</p>
<div style="padding: 2rem; border: 2px dashed"
     ondragenter="event.preventDefault()"
     ondragover="event.preventDefault(); event.dataTransfer.dropEffect = 'copy'"
     ondrop="event.preventDefault(); document.getElementById('result').textContent = event.dataTransfer.getData('text/plain')">Drop here</div>
<p id="result" role="status">Waiting</p>
</html>

Result

HELLO

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources