Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

ondragenter — handling entry of a dragged item

The ondragenter attribute defines JavaScript that runs for the dragenter event when a dragged item enters a target element. It lets the target react to entry but does not handle the drop itself.

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

Syntax

ondragenter="JavaScript statements"

ondragenter is an event handler attribute for dragenter, available on HTML elements. Its value contains JavaScript statements, not merely a function name: calling a function requires parentheses. Inside the attribute code, event refers to the supplied event. The corresponding JavaScript property, element.ondragenter, accepts a function or null.

dragenter signals that a dragged item has entered a target; it does not mean a drop occurred. The event bubbles and is cancelable. In the HTML Standard’s processing model, canceling dragenter is one way to set the current target for the drag-and-drop operation. In practice, current browsers do not require dragenter to be canceled for a drop to occur. To make a custom target accept a drop, call event.preventDefault() in the dragover handler and handle the drop in the drop handler. Unlike ondragenter, ondragover runs repeatedly while the item remains over the target.

The example dispatches a synthetic dragenter event to make the result repeatable without manual dragging. After clicking Run test, the output is dragenter: true: this confirms only that the call to event.preventDefault() in the attribute code canceled the dispatched event. The test does not verify real dragging, drop acceptance, or data transfer. Page security policies can block attribute-based code; an alternative is addEventListener('dragenter', handler).

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>ondragenter demo</title>
<div id="target" ondragenter="event.preventDefault(); document.getElementById('result').textContent = event.type + ': ' + event.defaultPrevented;">Target</div>
<button id="test" type="button">Run test</button>
<output id="result"></output>
<script>
  document.getElementById('test').addEventListener('click', () => {
    document.getElementById('target').dispatchEvent(
      new DragEvent('dragenter', { bubbles: true, cancelable: true })
    );
  });
</script>
</html>

Result

dragenter: true

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