CKTIS / HTML
ondragstart — handle the start of dragging
The ondragstart attribute defines JavaScript that runs for the dragstart event when dragging begins. It can prepare the operation's data or cancel its initiation; it does not itself make an element draggable.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
ondragstart="JavaScript statements"ondragstart is an event handler content attribute available on HTML elements. Its value is JavaScript code, not a Boolean value. It handles dragstart at the beginning of a drag operation, rather than every pointer movement.
Inside the attribute code, event is the DragEvent object. During a native dragstart, the drag data store is readable and writable: event.dataTransfer.setData('text/plain', 'Hello') sets the transferred text. Calling event.preventDefault() or executing return false in this handler cancels the initiation of dragging.
ondragstart alone does not enable dragging. For an ordinary source element, you can set draggable="true". ondragstart is also the name of a JavaScript property that accepts a function; with addEventListener, the event name is dragstart, without the on prefix.
The example deliberately cancels the operation after changing the status message. It demonstrates handling the start of dragging, not a complete drop implementation.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>ondragstart example</title>
<p draggable="true"
ondragstart="document.getElementById('status').textContent = 'dragstart: canceled'; event.preventDefault();">
Drag this text.
</p>
<p id="status" role="status">Ready</p>
</html>Result
dragstart: canceled
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.