Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add tinymce 4.x dynamically to textarea?

I have a little problem with adding tinymce dynamically to textarea after init.

tinymce.init({
    selector: "textarea",
    theme: "modern",
    height: 100,
    plugins: [
         "advlist autolink image lists charmap print preview hr anchor pagebreak spellchecker",
         "link searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
         "save table contextmenu directionality emoticons template paste textcolor"
   ],
   toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | l      ink image | print preview media fullpage | forecolor backcolor emoticons", 
   style_formats: [
        {title: 'Bold text', inline: 'b'},
        {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
        {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
        {title: 'Example 1', inline: 'span', classes: 'example1'},
        {title: 'Example 2', inline: 'span', classes: 'example2'},
        {title: 'Table styles'},
        {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
    ]
 }); 

And my button to add new textarea:

$('#add_new_text').click(function(){
var n = 1;
$( '<textarea class="cla" name="text'+n+'"></textarea>' ).appendTo( '#wrap_f' );
n++;
   })

I've tried to do this tinyMCE.execCommand('mceAddControl', false, ''); But it didn't work.

like image 556
Igor Le Avatar asked Mar 17 '23 03:03

Igor Le


1 Answers

Have you tried calling tinymce.init function after the appendTo function?

jQuery functions operates synchronously, one at a time. That means, your init function will run after the appentTo completes, and that means, you don't need a callback for that.

Just write your tinymce.init funtion after the appendTo and get back here to tell the result :)

like image 134
ilter Avatar answered Mar 24 '23 01:03

ilter