Getting, setting, and saving editor content
Use the editor.getContent() and editor.setContent() APIs to read and write editor content from JavaScript. Use these APIs instead of reading or writing the editor’s Document Object Model (DOM) directly, so that TinyMCE can filter, sanitize, and track the content.
Getting editor content
editor.getContent() returns the editor content as HTML, with editor-only markup removed. For the available arguments, see: getContent().
const html = tinymce.activeEditor.getContent();
Setting editor content
editor.setContent() replaces the entire editor content. Before the content is set, TinyMCE parses it, filters it against the editor schema, and sanitizes it. For information on content filtering and sanitization, see: Content filtering and xss_sanitization. For the API reference, see: setContent().
When the editor has focus, setContent() moves the caret to the start of the new content.
tinymce.activeEditor.setContent('<p>New content</p>');
To insert content at the current selection instead of replacing the entire editor content, use insertContent().
Arguments for setContent()
setContent() accepts an optional second argument, an object with the following properties.
| Property | Type | Default value | Description |
|---|---|---|---|
|
|
|
When |
|
|
|
When |
Editor features and plugins that respond to the SetContent event do not run for content set with no_events: true. For example, visual aids are not applied to the new content, and automatic image uploads are not started.
|
Adding an undo level
setContent() does not add an undo level, so the change is not recorded in the undo history. To allow users to undo the change, call undoManager.add() after setting the content.
const editor = tinymce.activeEditor;
editor.setContent('<p>New content</p>');
editor.undoManager.add();
Changing content before it is set
To change content before TinyMCE sets it, update the content property in a BeforeSetContent event handler. The updated content is still parsed, filtered, and sanitized before it is set. To cancel the change, call preventDefault() on the event. The SetContent event is still dispatched when the change is canceled.
Changing the content property in a SetContent event handler has no effect, as the content has already been set. The content property of the SetContent event is deprecated.
The BeforeSetContent event is also dispatched when content is inserted at the selection, and when undo and redo restore content. When content is inserted at the selection, the selection property of the event is true. Write the handler so that running it more than once on the same content does not change the result.
Example: replacing a placeholder before content is set
tinymce.init({
selector: 'textarea', // change this value according to your HTML
setup: (editor) => {
editor.on('BeforeSetContent', (e) => {
if (!e.selection) {
e.content = e.content.replace(/\{\{year\}\}/g, String(new Date().getFullYear()));
}
});
}
});
What to avoid when getting or setting content
-
Do not write HTML directly into the editor after it has loaded. For example, do not set
editor.getBody().innerHTML, the body of the editor’siframe, or the element used by an inline editor. Content written this way skips parsing, filtering, and sanitization, does not dispatch theBeforeSetContentorSetContentevents, and does not add an undo level. Useeditor.setContent()oreditor.insertContent()instead. Setting HTML on the target element before the editor loads is supported. -
Do not read HTML directly from the editor. For example, do not read
editor.getBody().innerHTML. The editor DOM contains editor-only markup, such asdata-mce-boguselements and editor-only classes. This markup can display incorrectly when the content is published, and can cause unexpected results when the content is loaded back into the editor. Useeditor.getContent()instead.
HTML forms options
hidden_input
The hidden_input option allows the auto-generation of hidden input fields to be disabled for inline editing elements. By default all inline editors have a hidden input element in which content gets saved when an editor.save() or tinymce.triggerSave() is executed.
The hidden_input option can be disabled if you don’t need these controls.
Type: Boolean
Default value: true
Possible values: true, false
Adding save functionality to the editor
TinyMCE can be configured to allow users to save the editor content. For information on configuring the user saving, see: The Save plugin.
Autosaving the editor content
TinyMCE can be configured to automatically save the editor content. For information on configuring the automatic saving, see: The Autosave plugin.