Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

ondragleave — handling departure from a drag target

The ondragleave attribute contains JavaScript that handles the dragleave event. It can, for example, remove highlighting from the previous target during a drag; it does not mean that the entire drag operation has ended.

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

Syntax

ondragleave="JavaScript statements"

ondragleave is an event handler attribute available on HTML elements. Its value is a JavaScript function body, not an event name or a Boolean value. It corresponds to the dragleave event; inside the attribute code, event represents the event and this refers to the element handling it.

The browser sends dragleave to the previous target element when the drag target changes; it can also occur when the operation is canceled. Do not confuse it with dragend, which concerns the source and the end of dragging. dragleave bubbles and is not cancelable: preventDefault() cannot stop departure from the target. With nested elements, account for events originating from descendants.

The HTML attribute holds code as text; the element.ondragleave property takes a function or null. Another option is addEventListener('dragleave', handler). Content Security Policy can block code in attributes.

The example deliberately dispatches a synthetic event when Test is clicked, making the result repeatable without mouse dragging. It checks handler execution, not the native drag-and-drop mechanism.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>ondragleave test</title>
<div id="zone" ondragleave="this.textContent = 'dragleave received'">Waiting</div>
<button type="button" id="test">Test</button>
<script>
  const zone = document.getElementById('zone');
  document.getElementById('test').addEventListener('click', () => {
    zone.dispatchEvent(new DragEvent('dragleave', {
      bubbles: true,
      cancelable: false
    }));
  });
</script>
</html>

Result

dragleave received

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