Tinymce get content

13,166

Solution 1

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

Solution 2

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();
Share:
13,166
InfinityGoesAround
Author by

InfinityGoesAround

Updated on June 04, 2022

Comments

  • InfinityGoesAround
    InfinityGoesAround almost 2 years

    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