All documentation ↓← All docs

editor.getHTMLCode()

Returns the editor’s content as an HTML string — the clean markup you save to your database or send to your API. It is the counterpart to setHTMLCode(), and is also available under the alias editor.getHTML().

Syntax

editor.getHTMLCode()

Parameters

None.

Returns

The content as an HTML string.

Behavior notes

  • Trailing empty blocks are trimmed. A dangling empty paragraph left by the caret at the end of the document is not included, so saved content does not accumulate stray blank blocks.
  • It respects code view. While the editor is showing HTML source, getHTMLCode() returns exactly what is in the code view, so you always read what the user currently sees.
  • Text and comments are preserved.Element markup, HTML comments, and encoded text nodes are all included — the result is faithful, publish-ready HTML.

Example usage

Save the content on submit:

Code example
var editor1 = new RichTextEditor("#div_editor1");
document.getElementById("btn_save").onclick = function () {
    var html = editor1.getHTMLCode();
    fetch("/api/save", { method: "POST", body: html });
};

Round-trip content in and back out:

Code example
editor1.setHTMLCode("<p>Hello <b>world</b></p>");
var html = editor1.getHTMLCode();   // "<p>Hello <b>world</b></p>"

Related

Calling editor.getHTMLCode() from your app

editor.getHTMLCode() belongs to the editor instance. Every integration below works the same way: keep the object that new RichTextEditor(...) returns, and call it on that object.

Plain JavaScript

Code example
var editor1 = new RichTextEditor("#div_editor1");
// The constructor is synchronous: the editor is ready on the next line.
editor1.getHTMLCode();

React and Next.js

Code example
// editorRef is filled by the RichTextEditor wrapper from the React guides.
const editorRef = useRef(null);

function onClick() {
  editorRef.current?.getHTMLCode();
}

return <RichTextEditor editorRef={editorRef} />;

In Vue and Angular, the wrapper components in the guides keep the instance in a component variable (editor), so the call is the same. See the React, Vue and Angular pages, or download a tested project from the guides.

If the editor is undefined

  • The call ran before the component mounted. Call it from an event handler or after the mount effect has run.
  • In React development mode, Strict Mode mounts, unmounts and mounts again, so the wrapper sets editorRef.current to null in between. Use editorRef.current?. rather than storing the instance elsewhere.
  • The editor was destroyed (editor.destroy()) when its component unmounted.

All methods: method reference · all events: event reference.