contenteditable
The HTML attribute that makes an element directly editable, and the surface behind most rich-text editors.
contenteditable is an HTML attribute that makes an element and its descendants editable in place. It is the browser primitive behind every rich-text editor, and it is notoriously awkward, because it hands you the browser's native editing behavior with very little control over it.
Its difficulties are consistent across browsers even where the behavior differs. Pressing Enter inserts markup whose shape is browser-defined. Pasting brings arbitrary HTML. Selection and caret position live in the Document Object Model and are destroyed by any re-render. The native undo stack is only fed by real user input, so programmatic changes are invisible to it — and synthetic events dispatched by test tooling are not real user input, which means editor behavior frequently cannot be driven from a test at all without using the browser's own command interface.
Two rules save the most time. Own the behavior you care about: intercept the key, prevent the default, and perform the edit through an undoable command rather than hoping the native behavior matches. And read the raw text, not the rendered text — a field displayed in uppercase by a style rule will save as uppercase if read through the rendered-text property.
A related discipline: whatever element carries an editable value must contain exactly that value. Decorative characters rendered inside it — quotation marks around a quote, a currency symbol — are read back on save and accumulate.
See also4
Document Object Model
The live tree of objects a browser builds from a document, and the surface every UI framework writes to.
Web Platform18 connections
Hydration
Attaching client-side behavior to server-rendered markup without rebuilding it.
Web Platform13 connections
Focus Management
Deliberately controlling which element receives keyboard focus as an interface changes.
Design & Interface14 connections
Headless Browser
A real browser driven programmatically without a visible window.
Testing & Verification10 connections
Related6
Nearby in the graph rather than deliberately chosen. Looser, sometimes surprising.
Linked from6
- Component StoryTesting & Verification
An isolated rendered instance of a component, usable as documentation, a workbench, and a test.
- Document Object ModelWeb Platform
The live tree of objects a browser builds from a document, and the surface every UI framework writes to.
- Focus ManagementDesign & Interface
Deliberately controlling which element receives keyboard focus as an interface changes.
- Headless BrowserTesting & Verification
A real browser driven programmatically without a visible window.
- HydrationWeb Platform
Attaching client-side behavior to server-rendered markup without rebuilding it.
- Reduced MotionDesign & Interface
An operating-system preference signaling that animation should be minimized, for real medical reasons.