最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - tinyMCE get editor returns null - Stack Overflow

programmeradmin4浏览0评论

I initialize 2 tinyMCE editor on 2 textarea with different id :

var variable_array = {id:'cName', test:'mon test'};
tinymce.init({
    selector: "#model_editor",
    entity_encoding : "raw",
    encoding: "UTF-8",
    theme: "modern",
    height: "500px",
    width: "100%",
    variables_list : variable_array,
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern modelinsert"
    ],
    toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image print preview media | forecolor backcolor emoticons",
    toolbar2: "variable_insert | question_insert",
    image_advtab: true,
    templates: [
        {title: 'Test template 1', content: 'Test 1'},
        {title: 'Test template 2', content: 'Test 2'}
    ]
});

tinymce.init({
    selector: "#headerfooter_editor",
    entity_encoding : "raw",
    encoding: "UTF-8",
    theme: "modern",
    height: "500px",
    width: "100%",
    variables_list : variable_array,
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern modelinsert"
    ],
    toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image print preview media | forecolor backcolor emoticons",
    toolbar2: "variable_insert | question_insert",
    image_advtab: true,
    init_instance_callback : "mceInitInstance",
    templates: [
        {title: 'Test template 1', content: 'Test 1'},
        {title: 'Test template 2', content: 'Test 2'}
    ]
});

Both editors init correctly. Then in order to set different content on each, I try to get the editor instance object id :

var editor_id = tinyMCE.get('#headerfooter_editor');
console.log(editor_id);

It returns null :/

I try also to get in the console the result of the callback of the second init :

function mceInitInstance(inst) {

console.log("Editor: " + inst.editorId + " is now initialized.");

And it returns : Editor: undefined is now initialized.

I want to do the following :

tinyMCE.get('#headerfooter_editor').setContent(data.content);

but of course it returns an error : Uncaught TypeError: Cannot read property 'setContent' of null

I don't understand what's wrong and why I can't get the editor instance id :/

I initialize 2 tinyMCE editor on 2 textarea with different id :

var variable_array = {id:'cName', test:'mon test'};
tinymce.init({
    selector: "#model_editor",
    entity_encoding : "raw",
    encoding: "UTF-8",
    theme: "modern",
    height: "500px",
    width: "100%",
    variables_list : variable_array,
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern modelinsert"
    ],
    toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image print preview media | forecolor backcolor emoticons",
    toolbar2: "variable_insert | question_insert",
    image_advtab: true,
    templates: [
        {title: 'Test template 1', content: 'Test 1'},
        {title: 'Test template 2', content: 'Test 2'}
    ]
});

tinymce.init({
    selector: "#headerfooter_editor",
    entity_encoding : "raw",
    encoding: "UTF-8",
    theme: "modern",
    height: "500px",
    width: "100%",
    variables_list : variable_array,
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern modelinsert"
    ],
    toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image print preview media | forecolor backcolor emoticons",
    toolbar2: "variable_insert | question_insert",
    image_advtab: true,
    init_instance_callback : "mceInitInstance",
    templates: [
        {title: 'Test template 1', content: 'Test 1'},
        {title: 'Test template 2', content: 'Test 2'}
    ]
});

Both editors init correctly. Then in order to set different content on each, I try to get the editor instance object id :

var editor_id = tinyMCE.get('#headerfooter_editor');
console.log(editor_id);

It returns null :/

I try also to get in the console the result of the callback of the second init :

function mceInitInstance(inst) {

console.log("Editor: " + inst.editorId + " is now initialized.");

And it returns : Editor: undefined is now initialized.

I want to do the following :

tinyMCE.get('#headerfooter_editor').setContent(data.content);

but of course it returns an error : Uncaught TypeError: Cannot read property 'setContent' of null

I don't understand what's wrong and why I can't get the editor instance id :/

Share Improve this question edited Nov 19, 2015 at 20:34 Vincent Teyssier asked Nov 19, 2015 at 20:10 Vincent TeyssierVincent Teyssier 2,2175 gold badges30 silver badges68 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

Your editors should be available using tinymce.get('model_editor') and tinymce.get('headerfooter_editor').

Hint: tinymce.editors holds all editor instances that have been initialized. You can loop through that array to get them all:

for (var i = 0; i < tinymce.editors.length; i++)
{
    console.log("Editor id:", tinymce.editors[i].id);
}        

Instead of:

tinyMCE.get('#headerfooter_editor').setContent(data.content);

use

tinyMCE.get('headerfooter_editor').setContent(data.content);

remove #

I got the same problem. The error message was:

TypeError: tinymce.get(...) is null

But my error was that I tried to tinymce.get(...) before initiating tinymce editor.

tinymce.init({selector: "#mytextarea"})
发布评论

评论列表(0)

  1. 暂无评论