CKTIS / HTML
ondrop — handling dropped data
The ondrop attribute defines JavaScript that handles the drop event during drag and drop. It does not create a drop target by itself: the default handling of dragover usually needs to be canceled as well.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
ondrop="JavaScript statements"
element.ondrop = function (event) { /* handler */ };ondrop is a global HTML event handler attribute: it can appear on any HTML element. Its value is JavaScript executed when a drop event is received, meaning data has been dropped onto an accepted target. The event object exposes the event, and event.dataTransfer exposes the transferred data.
An ondrop handler alone does not establish a typical drop target. Call event.preventDefault() in the dragover handler to allow dropping. The example also cancels dragenter. In the drop handler, use event.preventDefault() again to take over the default action, and read text with event.dataTransfer.getData('text/plain').
Distinguish the HTML attribute ondrop, whose value is source text, from the DOM property element.ondrop, which accepts a function or null. The event name is drop, not ondrop; use drop with addEventListener. Assigning element.ondrop again replaces the previous function registered through that mechanism.
The example transfers the fixed text Hello and displays it through textContent, without interpreting the data as HTML. It requires an environment that permits scripts and inline event handlers; document security restrictions can block them.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>ondrop example</title>
<p draggable="true"
ondragstart="event.dataTransfer.setData('text/plain', 'Hello'); event.dataTransfer.effectAllowed = 'copy'">Drag Hello</p>
<div ondragenter="event.preventDefault()"
ondragover="event.preventDefault(); event.dataTransfer.dropEffect = 'copy'"
ondrop="event.preventDefault(); document.getElementById('result').textContent = event.dataTransfer.getData('text/plain')"
style="padding: 2rem; border: 2px dashed">Drop here</div>
<p id="result">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.