Add tinymce to new textarea dynamically
Asked Answered
S

2

9

How can i do this

function addTinyText(id, text){
//add textarea to DOM
$('<textarea id="txt'+id+'"></textarea>').append(text).appendTo('body');
//init tineMCE
 tinyMCE.init({
        theme : "advanced",
        plugins : "emotions,spellchecker"
});
//add tinymce to this
tinyMCE.execCommand("mceAddControl", false, 'txt'+id);
}

but as a result every time we have a new textarea+tinyMCE but no text inside

What do i wrong?

Splinter answered 1/2, 2012 at 6:14 Comment(8)
this works well on my system (FF9), which browser are you using?Eupepsia
my browser is Chrome 16 -- and my code really works well on it. mb any conflicts wuz thereSplinter
problem, if text contains tags as <p></p><img /> and othersSplinter
can you show me what the variable text holds? (i think i might now what's wrong here)Eupepsia
mb solution is tinyMCE.get('textareaId').setContent('<p>html</p>') after tinymce load. Is there onload trigger?Splinter
you may also use your code inside a document.ready blockEupepsia
I try to set this function as a callback of a button and I wish that the area was created when I clicked on the button if I make button 1 (create textarea with mce) and button 2 (set content to textarea). after that i'll click on 1st button, wait for tinymce is loaded, than click on the 2nd button -- everything will be okSplinter
ok, so where do you still encounter problems?Eupepsia
S
1

line

$('<textarea id="txt'+id+'"></textarea>').append(text).appendTo('body');

should be line

$('<textarea id="txt'+id+'"></textarea>').text(text).appendTo('body');
Splinter answered 3/2, 2012 at 9:19 Comment(0)
B
3

I searched a very long time to find an answer to a similar problem.

My content was not being posted even thou I could get the editor to appear.

I got it working like this:

    jQuery('.wysiwyg').tinymce({
      // Location of TinyMCE script
      script_url : 'path/tinymce/tiny_mce.js',

      // General options
      theme : "advanced",
     .........
     .........
     ......... etc

I used the standard jquery code to initiate all text areas with class name wysiwyg as tinymce objects.

After ajax call completes and new textarea is loaded I run this function:

jQuery(".wysiwyg").each(function(){ 
    tinyMCE.execCommand("mceAddControl",false, this.id);
});

Now my code is finally being posted correctly.

Birthday answered 18/12, 2012 at 13:18 Comment(2)
@DentraAndres Thank you very much. This is a big fail on all tutorials and blog posts on this subject. Important detail if you are trying to dynamically initialize a TinyMCE textarea..Claraclarabella
For other people who read this: I was running TinyMCE 4.0.28, which required me to change mceAddControl to mceAddEditor. This is true for the current version of TinyMCE (5.2.0-75) as well, as of Mar 16, 2020Ikon
S
1

line

$('<textarea id="txt'+id+'"></textarea>').append(text).appendTo('body');

should be line

$('<textarea id="txt'+id+'"></textarea>').text(text).appendTo('body');
Splinter answered 3/2, 2012 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.