setContent of an textarea with tinyMCE
Asked Answered
I

11

21

I have some textareas and all of them are with tinyMCE.

I would like to set the content of the specific textarea, but I can't find how.

I have tryed this:

 tinyMCE.get('title').setContent(selected_article_title);

here is my textarea:

<textarea style="width: 95%;" name="Title"  id="title"></textarea>

And here my tinyMCE init:

tinyMCE.init({
// General options
mode : "specific_textareas",
theme : "advanced",
width: "100%",
plugins : "pagebreak,paste,fullscreen,visualchars",

// Theme options
theme_advanced_buttons1 : "code,|,bold,italic,underline,|,sub,sup,|,charmap,|,fullscreen,|,bullist,numlist,|,pasteword",
theme_advanced_buttons2 :"",
theme_advanced_buttons3 :"",
theme_advanced_buttons4 :"",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
valid_elements : "i,sub,sup",
invalid_elements : "p, script",
editor_deselector : "mceOthers"
});

I don't know why this is not working, I am using the exemple from the tinyMCE website http://www.tinymce.com/wiki.php/API3:method.tinymce.Editor.setContent

Ichnography answered 2/8, 2012 at 15:22 Comment(0)
I
14

I have the solution (thans to Thariama who gives me some elements)

To set the content of an textarea using tinyMCE, we heve to fill in the textarea before init the tinyMCE. Also, the response is as follows:

  1. Create the textarea:

    <textarea style="width: 95%;" name="Title"  id="title"></textarea>
    
  2. Set the content of the textarea:

    $('#title').html(selected_article_title);
    
  3. Init the tinyMCE:

    tinyMCE.init({
    // General options
    mode : "specific_textareas",
    theme : "advanced",
    width: "100%",
    plugins : "pagebreak,paste,fullscreen,visualchars",
    
    // Theme options
    theme_advanced_buttons1 : "code,|,bold,italic,underline,|,sub,sup,|,charmap,|,fullscreen,|,bullist,numlist,|,pasteword",
    theme_advanced_buttons2 :"",
    theme_advanced_buttons3 :"",
    theme_advanced_buttons4 :"",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    valid_elements : "i,sub,sup",
    invalid_elements : "p, script",
    editor_deselector : "mceOthers"
    });
    

And it's done ! Enjoy.

Ichnography answered 3/8, 2012 at 6:36 Comment(0)
G
19

I think this will solve your problem

it works fine for TinyMCE v:4..

// Sets the HTML contents of the activeEditor editor
tinyMCE.activeEditor.setContent('<span>some</span> html');

// Sets the raw contents of the activeEditor editor
tinyMCE.activeEditor.setContent('<span>some</span> html', {format : 'raw'});

// Sets the content of a specific editor (my_editor in this example)
tinyMCE.get('my_editor').setContent(data); // here my_editor is the id of a specific editor

// Sets the bbcode contents of the activeEditor editor if the bbcode plugin was added
tinyMCE.activeEditor.setContent('[b]some[/b] html', {format : 'bbcode'});

the link for the code is TinyMCE setContent

Glaciate answered 2/12, 2015 at 3:2 Comment(0)
I
14

I have the solution (thans to Thariama who gives me some elements)

To set the content of an textarea using tinyMCE, we heve to fill in the textarea before init the tinyMCE. Also, the response is as follows:

  1. Create the textarea:

    <textarea style="width: 95%;" name="Title"  id="title"></textarea>
    
  2. Set the content of the textarea:

    $('#title').html(selected_article_title);
    
  3. Init the tinyMCE:

    tinyMCE.init({
    // General options
    mode : "specific_textareas",
    theme : "advanced",
    width: "100%",
    plugins : "pagebreak,paste,fullscreen,visualchars",
    
    // Theme options
    theme_advanced_buttons1 : "code,|,bold,italic,underline,|,sub,sup,|,charmap,|,fullscreen,|,bullist,numlist,|,pasteword",
    theme_advanced_buttons2 :"",
    theme_advanced_buttons3 :"",
    theme_advanced_buttons4 :"",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    valid_elements : "i,sub,sup",
    invalid_elements : "p, script",
    editor_deselector : "mceOthers"
    });
    

And it's done ! Enjoy.

Ichnography answered 3/8, 2012 at 6:36 Comment(0)
M
13

For tinymce version 4,

tinymce.get('title').setContent(selected_article_title);

works just fine - also after initializing the editor.

Miffy answered 22/7, 2015 at 20:35 Comment(0)
M
4

In TinyMCE 5, you can do this using the setContent() method.

Let’s say you have initialized the editor on a textarea with id=”myTextarea”. First access the editor using that same id, then call setContent(). For example:

tinymce.get('myTextarea').setContent('<p>Hello world!</p>');

Or, instead of accessing the editor by id, you can access the active editor:

tinymce.activeEditor.setContent('<p>Hello world!</p>');

More info and examples here: https://www.tiny.cloud/blog/how-to-get-content-and-set-content-in-tinymce.

Matchlock answered 27/4, 2020 at 6:35 Comment(1)
'#myTextarea' didn't work. Reference it without the jquery-style selector. i.e. just 'myTextarea'Hurt
F
3

Using this

tinyMCE.get('title').setContent(selected_article_title);

won't work. It will set your editor content.

To set the editor source html element (the textarea) you will need to set it directly using

$('#title').html(selected_article_title);

You need to be aware that your editor is not the same thing as the textarea!

Fatherhood answered 2/8, 2012 at 15:58 Comment(2)
Thank you @Thariama, but this is not working for me. My textarea is still empty.Ichnography
8 years later, this has saved my day. The second line of code has updated the textarea, and focusses into the Text view.Royalroyalist
F
1

I'm a bit late to the party, but since tinyMCE version 5 you should use:

tinymce.activeEditor.execCommand('mceInsertContent', false, 'My new content');
Fagen answered 10/8, 2022 at 12:1 Comment(0)
D
1

All given answers won't work for me (using v6.4.2). I think the content is set too early and even if putting code after initialization of TinyMCE the change won't get noticed at all.

The workaround is not too dirty luckily. After trying some timeouts I noticed it'll only work after a while. If you set the content in the init_instance_callback option, it will work definitely.

Example:

const content = "..."
const textarea = document.querySelector('#title');
tinymce.init({
  target: textarea,
  ...
  init_instance_callback(editor) {
    editor.setContent(content);
  }
});
Dismember answered 26/4, 2023 at 15:34 Comment(1)
You got it. If you need to set the content after the init, instead of setTimeout, async + await logic will work. E.g. on an async function use await tinymce.init( and later tinyMCE.get('title').setContent( will work.Veach
H
0

If you set a content which contains mso tags, (a html content generated from outlook2013, which contains numbered list for example), you loose the list elements. Setting through tinymce.activeEditor.setContent(foo) or first setting textarea content and then initializing tinymce gives the same result, we can not see list elements properly, they are left aligned. But if we set using setContent(foo, { format:'raw' }) wee see the list elements properly. Why?

Happening answered 1/9, 2015 at 8:19 Comment(0)
I
0

The following works with tinymce version 4, and doesn't show the editor as a textarea while it's being dragged:

function initializeEditors() {
    var editorContent = {};
    $("#photo-list").sortable({
        start: function (e, ui) {
            $('.attachment-info').each(function () {
                var id = $(this).attr('id');
                editorContent[id] = tinyMCE.get(id).getContent();
            });
        },
        stop: function (e, ui) {
            $('.attachment-info').each(function () {
                var id = $(this).attr('id');
                tinymce.execCommand('mceRemoveEditor', false, id);
                tinymce.execCommand('mceAddEditor', true, id);
                tinyMCE.get(id).setContent(editorContent[id]);
            });
        }
    });
}
Intercut answered 19/10, 2016 at 16:50 Comment(0)
S
0
$('#title').html(selected_article_title);

Make sure that selected_article_title does not contain any html tags.

Spanjian answered 13/7, 2017 at 19:35 Comment(0)
O
0

I just wrote this instead of setContent

const textarea = document.querySelector('#title')
textarea.innerHTML = selected_article_title

and it worked

Oculist answered 9/2, 2022 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.