contenteditable and Drag and Drop
How contenteditable makes any element an editor, what plaintext-only changes, and why native drag and drop needs dragover cancelled and a keyboard fallback.
contenteditable and Drag and Drop
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `contenteditable` makes an element editable and keyboard-focusable with no `tabindex`. Typing, selection and caret all work, but the markup the browser writes when the user presses Enter differs between engines.
- `contenteditable="plaintext-only"` blocks rich formatting. Enter produced a newline character in Chromium and a `<br>` in Firefox and WebKit builds in a test.
- Native drag and drop starts with `draggable="true"` and a `dragstart` handler that puts data in `event.dataTransfer`. Links and images are draggable by default.
- A drop target must cancel `dragover` with `preventDefault()`. Without that, the `drop` event never fired in a test and `dropEffect` ended as `none`.
- Drag and drop has no keyboard equivalent built in, so a draggable `div` is not accessible by itself. Provide buttons or another way to do the same job.
Two old attributes, one idea
Both of these make a normal element behave like a control. One turns any element into a text field, and the other lets the user pick something up and drop it somewhere. Both have been around since the first HTML5 drafts, and both have sharp edges.
contenteditable
<div contenteditable>Edit me</div>
That is the whole feature. In Chromium 159, Firefox 159 and a WebKit build, the element took focus from Tab with no tabindex, and typing and the caret worked. isContentEditable was true, and contenteditable="false" opted a nested element out.
The edge: what the browser writes. After pressing Enter at the end of “edit me TYPED” and typing “L2”, the markup came out as edit me TYPED<div>L2</div> in Chromium and the WebKit build, and <div>edit me TYPED</div><div>L2</div> in Firefox. If you read innerHTML back, that difference is yours to handle. For plain text, use contenteditable="plaintext-only": Enter produced "plain\nline2" in Chromium and plain<br>line2 in the other two. web-features lists plaintext-only as Baseline from 4 March 2025, when Firefox 136 shipped it.
Remember it is user input: escape or sanitise before storing or rendering it elsewhere, and give an editable region an accessible name (names lesson) and a role such as textbox when it acts as one. For anything beyond one-line notes, use a proper textarea or an editor library.
Native drag and drop
The API has three parts: make something draggable, put data on the drag, and accept it on a target.
<div id="src" draggable="true">drag</div>
<div id="dst">drop here</div>
<script>
src.addEventListener("dragstart", e => e.dataTransfer.setData("text/plain", "payload-42"));
dst.addEventListener("dragover", e => e.preventDefault()); // required
dst.addEventListener("drop", e => { e.preventDefault(); console.log(e.dataTransfer.getData("text/plain")); });
</script>
I drove this with real pointer drags in all three engines:
- The
dragstartfired, the target’sdropreceived “payload-42” anddragendreporteddropEffect=move. - On a second target with no
preventDefault()indragover,dropnever fired anddropEffectwasnone. This is the classic “my drop does nothing” bug: the browser only treats a target as accepting drops if you canceldragover. - An element without
draggabledid nothing when dragged. Links and images are draggable by default: dragging a link put its URL intodataTransfer, and in Firefox animggave its data URL.
Keyboard and everyone else
A native drag needs a pointer. In the test a draggable div was not focusable, and nothing in HTML starts a drag from the keyboard. Anything done by dragging, like reordering a list, must also be possible another way: buttons for “move up” and “move down”, or a select for the position. The focus lesson explains why custom controls must also work without a mouse. Touch is another gap: native drag and drop is awkward on phones, so libraries that rebuild it with pointer events exist.
What the video covers
This is remarkablemark’s “Drag and drop with HTML draggable” (January 2021, about 16 minutes). The description says it covers how native web drag and drop works with the HTML5 draggable attribute, and links a Repl.it demo and a blog post. It appears to be a livestream recording and is a small channel, so expect an unpolished pace. I could not read a transcript. It does not cover contenteditable; dcode’s short video on that is in further viewing, and the test results above are mine.
Where this fits
This ends the interactive-elements group, after custom elements. The final group is production concerns, starting with HTML security.
Check yourself
Your `drop` handler never runs. What is the most likely cause?
The target did not call `preventDefault()` in its `dragover` handler. In a test the drag with no cancel fired `dragover` repeatedly, `dragend` reported `dropEffect=none`, and `drop` never ran. Cancel `dragover` and the drop fires.
Is a `<div draggable="true">` keyboard accessible?
No. In a test it was not in the tab order, and there is no keyboard way to start a native drag. Offer an alternative such as move up and move down buttons, which also helps touch and voice users.
What does `contenteditable="plaintext-only"` give you over `contenteditable`?
It stops rich-text editing, so pasted or typed content stays plain text. The browsers disagree on line breaks: Chromium inserted a newline, Firefox and WebKit builds inserted `<br>`. Support reached Firefox in version 136, so it is newly Baseline.
Why do teams avoid building a rich editor straight on `contenteditable`?
The markup it generates varies between engines. In a test, pressing Enter produced `<div>` wrappers in Chromium and Firefox, with different surrounding structure, and Chromium left a bare text node. Editors like ProseMirror normalise that.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.