HTML Editable Content with contenteditable
Create an editable region
contenteditable is an enumerated attribute, not a boolean attribute. The value true or an empty value enables editing; false disables editing. A missing or invalid value normally inherits the parent editing state. Use a labeled textarea instead for ordinary multiline form input.
<p id="label">Draft note</p><div contenteditable="true" role="textbox" aria-labelledby="label" aria-multiline="true">Edit this note.</div>Click inside the note and type; it can be edited locally.
Restrict editing to plain text
The plaintext-only value allows text editing without rich formatting. A supporting browser strips formatting when pasting. A child with contenteditable="false" can protect a noneditable part of an editing region. These are interface features, not security boundaries.
<div contenteditable="plaintext-only" role="textbox" aria-label="Plain note" aria-multiline="true">Type plain text.</div><div contenteditable="true" role="textbox" aria-label="Draft"> <span contenteditable="false">Note:</span> Edit here.</div>The plain region reports plaintext-only, and the protected span is not editable.
Read text changes safely
An editable div is not a successful form control just because it has a name. To transfer its contents, application logic must read the edited value. This input event listener copies textContent into a preview. textContent inserts text, not executable markup. Persisting edits requires separate logic.
<div id="note" contenteditable="true" role="textbox" aria-label="Note">Draft</div><p> Preview: <span id="preview">Draft</span></p><script> const note = document.getElementById("note"); note.addEventListener("input", () => { document.getElementById("preview").textContent = note.textContent; });</script>Edit the draft; the preview shows its text. Formatting is not preserved.
Common mistakes and practice
Do not insert untrusted edited HTML into innerHTML. Plaintext-only does not replace validation or safe output encoding. For rich editors, plan keyboard interaction, labeling, sanitization and persistence explicitly. Nested editable regions are not automatically separate Tab stops.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Editable Content with contenteditable — Knowledge Check
Apply the interaction concepts taught in this lesson.
Is contenteditable a boolean attribute?
Which value disallows rich formatting?
What does a missing contenteditable value normally do?
Does an editable div automatically contribute form data?
Which property safely inserts a plain text preview?