👀 Live Preview
Start dragging the box. dragstart calls setData and updates status:
Waiting for dragstart…

The ondragstart attribute is an inline event handler that runs JavaScript when the dragstart event fires on the draggable source — the element being dragged. It fires once when the user begins dragging. It is part of the HTML Drag and Drop API: set draggable="true" on the source, then use ondragstart to call event.dataTransfer.setData(type, value) so drop targets can read the payload with getData. You can also set event.dataTransfer.effectAllowed and setDragImage for a custom drag ghost. The drag sequence on the source is dragstart → drag (many times) → dragend; drop targets receive dragenter, dragover, and drop. For production sites, prefer addEventListener("dragstart", …) over inline handlers.
Inline JS.
On source.
Transfer payload.
Permitted ops.
Preferred way.
drag/dragend/drop.
ondragstart AttributeThe primary purpose of ondragstart is to define what happens when a user begins dragging an element. Common uses include calling event.dataTransfer.setData() to attach a payload, setting event.dataTransfer.effectAllowed to declare copy or move intent, and updating a status label to confirm the drag has started. It connects the first moment of a drag gesture to JavaScript without a full page reload.
Do not confuse ondragstart with ondragover—that fires on the drop target while hovering. dragstart fires on the source exactly once. Unlike ondrag (repeated while moving) or ondragend (once when released), dragstart is where you must call setData for drop targets to receive data via getData. Avoid alert() in handlers; update visible status text instead.
Without draggable="true" on the source element, the browser will not fire dragstart and ondragstart never runs. Only certain elements (like links and images) are draggable by default.
Set ondragstart on a draggable source or assign element.ondragstart:
<script>
function handleDragStart(event) {
event.dataTransfer.setData("text/plain", "Hello from dragstart");
event.dataTransfer.effectAllowed = "move";
document.getElementById("status").textContent = "dragstart — setData called.";
}
</script>
<div draggable="true" ondragstart="handleDragStart(event)">Drag me</div>
<p id="status"></p>dragstart fires on the draggable source, not on drop targets.draggable="true" on the source element.event.dataTransfer.setData(type, value) so drop targets can read the payload.event.dataTransfer.effectAllowed to copy, move, link, or combinations.event.dataTransfer.setDragImage(element, x, y) for a custom drag ghost.element.ondragstart = function(event) { … }.element.addEventListener("dragstart", handler).The ondragstart attribute accepts a string of JavaScript code:
ondragstart="handleDragStart(event)" — Call a named function with the event object.ondragstart="event.dataTransfer.setData('text/plain', 'data')" — Minimal inline setData.source.ondragstart = (e) => { e.dataTransfer.setData(…); … } assigns the handler.event.dataTransfer.effectAllowed inside the handler to declare permitted operations.const source = document.getElementById("dragSource");
source.addEventListener("dragstart", (event) => {
event.dataTransfer.setData("text/plain", "Item payload");
event.dataTransfer.effectAllowed = "move";
document.getElementById("status").textContent =
"dragstart — setData enables drop target getData.";
});
document.getElementById("dragSource").ondragstart = function (event) {
event.dataTransfer.setData("text/plain", "Hello");
console.log("effectAllowed:", event.dataTransfer.effectAllowed);
};| Event | When it fires | Handler |
|---|---|---|
dragstart | Once when drag begins | ondragstart |
drag | Repeatedly while moving source | ondrag |
dragend | Once when drag ends (source) | ondragend |
drop | Once when released on target | ondrop |
setData | Required in dragstart for getData on drop | event.dataTransfer.setData(type, value) |
effectAllowed | Set during dragstart on source | copy, move, link, copyMove |
draggable | Required on source | draggable="true" |
| Target | Supported? | Notes |
|---|---|---|
<div>, <span> | Yes | Needs draggable="true"; common drag sources |
<li> | Yes | Sortable list items; setData with item id in dragstart |
<img>, <a> | Yes | Draggable by default in most browsers |
<button> | Yes | Needs draggable="true"; add keyboard alternative |
| Drop target | Separate | Use ondragover/ondrop on receivers, not dragstart |
ondragstart vs ondrag vs ondragend vs ondrop| Handler | When it fires | Element | Typical use |
|---|---|---|---|
ondragstart | Once at drag start | Draggable source | setData, effectAllowed, setDragImage |
ondrag | Repeatedly while moving | Draggable source | Live feedback, opacity, counters during drag |
ondragend | Once when drag ends | Draggable source | Reset styles, finalize source state |
ondrop | Once when released on valid target | Drop target | getData, move DOM node, accept payload |
Inline ondragstart with setData and status text, dynamic element.ondragstart assignment, and a full mini workflow where dragstart sets data and the drop zone reads it on drop.
Start dragging the box. dragstart calls setData and updates status:
Waiting for dragstart…
Call setData in ondragstart so drop targets can read the payload:
<div draggable="true" ondragstart="handleDragStart(event)">Drag me</div>
<p id="msg"></p>
<script>
function handleDragStart(event) {
event.dataTransfer.setData("text/plain", "Hello from dragstart");
event.dataTransfer.effectAllowed = "move";
document.getElementById("msg").textContent = "dragstart — setData called.";
}
</script>The browser fires dragstart once on the draggable source when the user begins dragging. The inline ondragstart attribute must call event.dataTransfer.setData() so drop targets can retrieve the payload.
Assign the handler with element.ondragstart or addEventListener:
<script>
document.getElementById("dynamicDraggable").ondragstart = function (event) {
event.dataTransfer.setData("text/plain", "Dynamic payload");
event.dataTransfer.effectAllowed = "copyMove";
document.getElementById("log").textContent =
"Dynamic dragstart — setData called.";
};
// Or preferred form:
document.getElementById("dynamicDraggable").addEventListener("dragstart", handler);
</script>Register the listener once at page load. When the user begins dragging, your handler runs once and sets the transfer data before any drop target can receive it.
Full mini workflow: dragstart on the source sets data; the drop zone calls getData on drop:
source.addEventListener("dragstart", function (event) {
event.dataTransfer.setData("text/plain", "Item from dragstart");
event.dataTransfer.effectAllowed = "move";
});
zone.addEventListener("dragover", function (event) {
event.preventDefault();
});
zone.addEventListener("drop", function (event) {
event.preventDefault();
var payload = event.dataTransfer.getData("text/plain");
status.textContent = "drop — getData: " + payload;
});dragstart initializes the drag operation on the source. Drop targets need preventDefault in dragover to accept the drop, then read the payload set during dragstart.
aria-live regions when dragstart updates status text so screen reader users know a drag has begun.Pointer moves on element with draggable="true".
Call setData, effectAllowed, optional setDragImage on source.
drag fires many times while pointer moves; dragend fires on release.
dragenter, dragover, drop on receivers; getData reads payload set in dragstart.
The dragstart event and ondragstart handler are part of the HTML Drag and Drop API and supported in all modern browsers — Chrome, Firefox, Safari, and Edge. Touch drag behavior may differ; test on your target devices.
ondragstart supportAll major browsers fire the underlying event and honor the ondragstart handler attribute.
Bottom line: Universal support for dragstart on draggable sources. Always set draggable="true", call setData in dragstart, and pair with drop target handlers.
ondragstart.The ondragstart attribute runs JavaScript once when a user begins dragging a draggable source and the dragstart event fires on that element. Its most important job is calling event.dataTransfer.setData() so drop targets can retrieve the payload.
Prefer addEventListener("dragstart", …), set event.dataTransfer.effectAllowed, pair with ondragover/ondrop on receivers, and offer keyboard alternatives for accessibility.
ondragstartBookmark these before wiring your next event handler.
dragstart on draggable.
EventFires one time.
BehaviorRequired for getData.
Use casePreferred.
ModernDeclare intent.
Comparedragstart event fires on a draggable source—once when the user begins dragging that element.event.dataTransfer.setData(type, value) in dragstart to attach a payload. Drop targets read it with getData in the drop handler; without setData the target receives nothing.ondragstart fires once when dragging begins and is where you set transfer data. ondrag fires continuously on the source while the pointer moves during the drag.effectAllowed is set on the source during dragstart to declare permitted operations: copy, move, link, or combinations like copyMove.element.addEventListener("dragstart", …). Inline ondragstart works for simple demos.ondragstart fires once on the draggable source when the drag begins. ondrop fires once on the drop target when the user releases the item and is where you read getData.Practice inline ondragstart, addEventListener, effectAllowed, and the full drag-to-drop workflow in the Try It editor.
5 people found this page helpful