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

javascript - Tinymce get content - Stack Overflow

programmeradmin11浏览0评论

I try to get the content of tinymce, like this:

 var hallo = tinyMCE.activeEditor.getContent();
            alert(hallo);

but every time I get this message:

Uncaught TypeError: Cannot read property 'getContent' of null

I am using tinymce 4.

Thank you

I try to get the content of tinymce, like this:

 var hallo = tinyMCE.activeEditor.getContent();
            alert(hallo);

but every time I get this message:

Uncaught TypeError: Cannot read property 'getContent' of null

I am using tinymce 4.

Thank you

Share Improve this question asked Jul 17, 2015 at 11:57 InfinityGoesAroundInfinityGoesAround 1,0214 gold badges17 silver badges33 bronze badges 5
  • Did you assign a textarea with a class and declare that with editor_selector? – Patel Commented Jul 17, 2015 at 12:04
  • Thank you for your answare. What do you mean? – InfinityGoesAround Commented Jul 17, 2015 at 12:06
  • 1 I just set this up for you. Check the link to see what I mean. ---> fiddle.tinymce.com/WSeaab/1 – Patel Commented Jul 17, 2015 at 12:10
  • 1 Did it help and solve you problem? – Patel Commented Jul 17, 2015 at 12:21
  • For this yes. But I put a other post to disable/enable button – InfinityGoesAround Commented Jul 17, 2015 at 12:23
Add a comment  | 

3 Answers 3

Reset to default 8

You can get tinyMCE content by calling the the method triggerSave in the following way

tinyMCE.triggerSave();

after declaring this method you can get the contents by selector for example:-

var contents = $("#myTextArea").val();

or

var contents = tinyMCE.get('myTextArea').getContent();

Cannot read property 'getContent' of null often means that TinyMCE is unable to find your textbox which means there is something wrong in the reference to textarea's class.

<form method="post" action="somepage">
    <textarea id="myTextArea" class="mceEditor">I should buy a boat. </textarea>
</form>

<button onclick="content()">Get content</button>

Take note of mceEditor class which we will now inform the TinyMCE editor about :

<script type="text/javascript">

    tinyMCE.init({
        mode : "specific_textareas",
        editor_selector : "mceEditor"   //<<<---- 
    });

</script>

And now simply get the contents of that textbox on the button click.

function content() {
    alert(tinyMCE.get('myTextArea').getContent());
}

Here is working DEMO

Further to Shabaz's answer...

For TinyMCE version 4.1.9, I discovered the following:

  1. tinyMCE.get('bobt').getContent(); and tinymce.get('bobt').getContent(); BOTH WORK (that is, uppercase the MCE does not matter)
  2. bobt in my example code is an ID, not a class
  3. The triggerSave() call is unnecessary - all that is important is for the textarea to have an ID, and to use the ID in the get('#YOUR_ID').getContent() call.

Full Example Code (non run-able):

$(function(){
  $('button').click(() => {
    //tinyMCE.triggerSave();
    const out3 = tinyMCE.get('bobt').getContent(); //*MUST* be an ID, not a class
    alert(out3);
    $('#out').html(out3);
  });
});
    <textarea id="bobt" class="tinymce"></textarea>
    <div id="outx"><button>Get it</button></div>
    <div id="out"></div>

发布评论

评论列表(0)

  1. 暂无评论