← Back to HTML & CSS, page 8

html-css-0160 · Medium mid

How does contenteditable work, and why is building an editor on it harder than it looks?

contenteditable lets the user edit an element, with no value property and no change event, so read the content and listen to input. It needs a role and a name, browsers differ in the markup they create, and pasted content must be cleaned. Use input or textarea for plain text.

Full answer

The contenteditable attribute makes an element editable by the user, with caret, selection, undo and typing support from the browser. Values are true (or empty), false, and plaintext-only, which allows editing but keeps the content as plain text. The isContentEditable property tells if an element is editable, including through inheritance.

It is useful for small things: an inline title that can be edited, a notes box, or a rich text area. Facts to know:

  • The element is focusable, so it is in the tab order. Give it a role and a name, such as role="textbox", aria-multiline="true" and aria-label, or connect a visible label with aria-labelledby. A bare div has no meaning for a screen reader.
  • There is no value and no change event. Read innerText or textContent, or innerHTML for markup, and listen to input events.
  • Browsers produce different HTML for the same action. Pressing Enter may add a div, a p or a br. Pasting brings in styles and markup from other pages.
  • Pasted content is a security risk: it can include unwanted markup. Intercept the paste event and insert plain text, or sanitize the result.
  • The old document.execCommand API for bold and italic is deprecated, and there is no complete replacement. Real editors, such as those built on ProseMirror or similar libraries, keep their own document model and only use the editable element for input.
  • A form does not submit its content. Copy it into a hidden field or send it with a script.

For plain text fields, an input or textarea is simpler, more accessible and works with forms.

Typing into the first box fires input events and the text appears in it. The second box accepts only plain text. The first box is reachable by the keyboard and announced as a text box with the name "Notes".

The paste handler uses execCommand("insertText"), which still works for this purpose in current browsers, but check the support before relying on it.

html
<div id="e" contenteditable="true" role="textbox" aria-multiline="true" aria-label="Notes" style="border:1px solid #999;padding:4px;min-width:200px"></div>
<div id="p" contenteditable="plaintext-only" role="textbox" aria-label="Plain" style="border:1px solid #999;padding:4px;min-width:200px"></div>
<script>
  window.count = 0;
  e.addEventListener("input", () => { count++; });
  e.addEventListener("paste", ev => {
    ev.preventDefault();
    const text = ev.clipboardData.getData("text/plain");
    document.execCommand("insertText", false, text);
  });
</script>

Checked with real key presses in Chrome. No screen reader was run, and touch input was not tested. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Values: true, false and plaintext-only.
  • Give it role=textbox and an accessible name.
  • Use the input event; there is no value or change.
  • Pasted markup is a risk; handle paste.
  • Prefer input and textarea for plain text.

Common mistakes

  • Using contenteditable for a simple text field instead of an input or textarea.
  • Forgetting a role and an accessible name.
  • Accepting pasted HTML from other pages without cleaning it.

Follow-ups

  • What does plaintext-only change?
  • Why is execCommand a poor base for a rich text editor?

Tips

  • State where it fits and where input or textarea is better.
  • Mention paste, Enter behaviour and the missing value and change event.