← All 93 demos

Plain Text Output

Get visible text from the document with getPlainText(). The editor still stores HTML.

How it works

The editor stores HTML, but getPlainText()returns just the visible text — formatting dropped, block structure surviving as line breaks. That is what you want for search indexing, previews, notification snippets, and any count a user would recognise.

getPlainText()

0 words · 0 characters — counted on the text, not the markup (0 characters of HTML).

Loading…

Example code

// Visible text, with block structure surviving as line breaks.
const text = editor.getPlainText();     // editor.getText() is an alias

// Character and word counts should measure TEXT, not markup:
const characters = text.length;
const words = text.trim() ? text.trim().split(/\s+/).length : 0;

// And the reverse — replace the document with plain text:
editor.setPlainText("Line one\nLine two");

Counting getHTMLCode().length instead is the common mistake: a short sentence wrapped in a few tags reads as hundreds of characters, and the number a user sees stops matching the one you enforce.

Why not just read the text content? Because textContent concatenates without regard for structure: three list items come back as AlphaBravoCharlie, and a search index then tokenises the last word of one item and the first of the next as a single term. getPlainText() is layout-aware, so the same list comes back as Alpha\nBravo\nCharlie— blocks separated by a blank line, list items by a single one.

getPlainText() reference