Initialize tinymce with content

I have a page containing one instance of the tinymce4 editor. I want to initialize this editor with some software. I know that I need to call: tinymce.get('editor').setContent('my content');

However, it is difficult for me to do this when tinymce initialization. This question has already been asked: initialize tinyMCE with default content , but the answer that was given at that time does not work, at least for tinymce4.

Here is what I tried:

First try:

  tinymce.init({ mode: "textareas", ... setup: function (editor) { ... editor.setContent('my content'); } }); 

-> Uncaught TypeError: cannot read the 'body' property from undefined

Second attempt:

  tinymce.init({ mode: "textareas", ... }; tinymce.get('editor').setContent('my content'); 

-> Uncaught TypeError: Cannot read the 'setContent' property from null (however, if I do this when the page containing the tinymce editor is already loaded, it works).

Third attempt (SO 12083361 answer):

 $(document).ready(function(){ tinymce.get('editor').setContent('my content'); }); 

-> Uncaught TypeError: Unable to read setContent property from null

All this does not work with tinymce.activeeditor.setContent('my content'); .

Where can I put tinymce.get('editor').setContent('my content'); in my code to make it work?

+5
source share
2 answers

With version 4, you should do it like this:

 tinymce.init({selector:'textarea'}); tinymce.activeEditor.setContent('custom'); 

Here's the fiddle .

+4
source

I used init_instance_callback, which starts when the editor is ready to installContent.

 var options = { ... }; options.init_instance_callback = function (editor) { editor.setContent(initialContent); }; $("some-selector").tinymce(options); 
0
source

Source: https://habr.com/ru/post/1206545/


All Articles