12

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!

TRiG
  • 10,148
  • 7
  • 57
  • 107
高科技黑手
  • 1,252
  • 2
  • 12
  • 20

7 Answers7

7

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');
});
6

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

Harsain
  • 121
  • 2
6
tinymce.activeEditor.setContent('<span>some</span> html');

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

Mike Barwick
  • 6,288
  • 6
  • 51
  • 76
Fury
  • 4,643
  • 5
  • 50
  • 80
6

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__);  
     }
});
Eric
  • 9,870
  • 14
  • 66
  • 102
0

This code runs as soon the document is in ready state, so the only thing is tinymce is setting the content even before textarea has content. One question where and how is the content being set in the textarea?

Secondly can you place tinyMCE.init({ .... }); in setTimeOut.

Hope this helps.

Harsain
  • 121
  • 2
  • can you try one thing before `tinyMCE.init({ ... })` try alerting the contents of the textarea. also did you try settimeout – Harsain Dec 05 '13 at 03:34
  • Thanx for the quick response; content in the is loaded while the page is generated: My program pulls content from DB and constructs web page using XSLT, therefore we should be confident that the content is static before any script accessing it. BTW, I think the order of execution is as the following: 1. Content, as well as HTML are generated at the same time (from the UA's perspective). 2. UA constructs DOM, when it's done, jQuery comes into the game. 3. tinymce.init(); initializes itself, but not yet providing an instance itself. (based on your answer, thanx again) – 高科技黑手 Dec 05 '13 at 03:44
0

I remember doing something like this once, manually adding the textarea value to TinyMCE:

tinyMCE.init({
   ...
   setup : function(editor) {
      editor.setContent($('TEXTAREA#rtf').text());
   }
});

The "editor" parameter for setup function is a TinyMCE instance, guaranteed to be there. Haven't used TinyMCE in a while, but I remember having numerous problems like this! You're not alone in finding issues here.

But why not use something like this? http://www.tinymce.com/wiki.php/Configuration3x:editor_selector

tinyMCE.init({
        ...
        mode : "specific_textareas",
        editor_selector : "TEXTAREA#rtf" // CSS Selectors
});

Which is the default way to use TinyMCE. It will convert your textareas into TinyMCE instances and keep the value synced between the input and TinyMCE

Jordan P
  • 1,499
  • 1
  • 10
  • 14
  • I've tried both approaches, neither works out for me; I don't know if this has anything to do w/ the TinyMCE version (4.0.11 to be exact), coz I don't find setup, mode, and editor_selector from official documention (under API 4.x). But I tried anywayz. Thanx to your answer, it helps me understand this lib more! – 高科技黑手 Dec 05 '13 at 04:08
  • Have you tried replicating their Installation tutorial exactly? http://www.tinymce.com/wiki.php/Installation (This is for API4.x) You're probably doing something out of order and the error messages aren't very helpful. – Jordan P Dec 05 '13 at 04:28
0

I've been doing it that way :

tinymce.init({
     ...
     setup: function (editor) {
        editor.on('init', function () {
            editor.setContent('<p>Some content goes here.</p>');
            tinymce.triggerSave();
        })
     },
     ...
});
Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57