Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set content in TinyMCE 4, preferably within the $(document).ready(); block?

As the title says, I've looked up the official documentation but it ain't working; here's my JavaScript (utilizing jQuery) code:

$(document).ready(function() {
    tinymce.init({
        element_format: "html",
        schema: "html4",
        menubar: false,
        plugins: 'preview textcolor link code',
        selector: 'TEXTAREA#rtf',
        toolbar: 'preview | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | blockquote subscript superscript | code'
    });
    tinymce.activeEditor.setContent($('TEXTAREA#rtf').text());
});
  1. I've tried to inspect tinymce and tinyMCE (got this from Googling) instances, and they both are objects alright.
  2. I've also tried to inspect tinymce.activeEditor and tinyMCE.activeEditor, but they turn out to be null!

(Cough) Somehow, after I restored everything back to where I started now it works:

$(document).ready(function() {
    tinymce.init({
        element_format: "html",
        menubar: false,
        plugins: 'preview textcolor link code',
        schema: "html4",
        selector: 'TEXTAREA#rtf',
        toolbar: 'preview | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | blockquote subscript superscript | code'
    });
});

I suspect it was due to either the UA cache or the XSLT transformed result cache that made the issue; thanks again for your time!

like image 891
高科技黑手 Avatar asked Dec 05 '13 03:12

高科技黑手


People also ask

How do I put content in TinyMCE?

How to setContent with multiple editors. Running the setContent() function on specifically editorOne with the tinymce. get('editorOne'). setContent(contentOne); method, you can change just editorOne.

How do I save HTML content of TinyMCE into a file?

To save the page, you have to insert TinyMCE in a HTML form and add a submit button. Then you'll have to handle the submitted form with a language like PHP. A call to the file_put_contents() function should do it.


4 Answers

This thread set me on the right track so I thought I'd share my solution with v4

tinymce.init({
     ...,
     init_instance_callback:function(editor){
         editor.setContent(__YOUR_CONTENT__);  
     }
});
like image 111
Eric Avatar answered Sep 29 '22 11:09

Eric


I have found better solution that works anywhere in code (and is not a hack, unlike the setTimeout trick)

tinymce.get('tinymce-element-id').on('init',function(e) {
      e.target.setContent('my custom content');
});
like image 29
Radomír Laučík Avatar answered Sep 29 '22 11:09

Radomír Laučík


tinymce.activeEditor.setContent('<span>some</span> html');

https://www.tinymce.com/docs/api/tinymce/tinymce.editor/#setcontent

like image 32
Fury Avatar answered Sep 29 '22 10:09

Fury


this is how I implemented it:

setTimeout(tinyMCE.init({
                            selector: "textarea.edit_notes",
                            height: editor_height,
                            theme: "modern",
                            menubar: "tools table format view insert edit",
                            force_br_newlines : false,
                            force_p_newlines : false,
                            forced_root_block : '',
                            //plugins: "fullpage",
                            valid_elements : '*[*]',
                            setup: function(ed){
                                ed.on("init",
                                      function(ed) {
                                        tinyMCE.get('testeditor').setContent($('#testeditor').val());
                                        tinyMCE.execCommand('mceRepaint');

                                      }
                                );
                            }
                            }), 50);

Give it a shot

like image 35
Harsain Avatar answered Sep 29 '22 12:09

Harsain