Search code examples
jqueryasp.net-mvctinymce-4

tinymce getcontent not working


tinymce.get('content').getContent 

is not working.

Here is my code. you can see all the different comments for different things I have tried. I have

alert('end');

at the end just as a test to see if the code is working. Currently it is not unless i comment out anything related to tinymce that is not the initializer. These functions are inside of document ready.

Why wont tinymce getcontent work?

<script>
    $(document).ready(function () {
        tinymce.init({
            selector: '#mytextarea',
            inline: true
        });
        //$('input').keyup(function () {
        //    //alert("keyed");
        //    $('[name="' + $(this).attr('name') + '"]').val($(this).val());
        //});        
        //$('.artBodyText').keyup(function () {
        //    alert("keyed");
        //    $('[name="' + $(this).attr('name') + '"]').val($(this).val());
        //});
        //$('#mytextarea').keyup(function () {
        //    alert("keyed");
        //    $('[name="' + $(this).attr('name') + '"]').val($(this).val());
        //});
      // var x = tinymce.get('#mytextarea').getContent();
        //var y = $('mytextarea').tinymce().getContent();
        alert(tinymce.get('mytextarea').getContent());
        alert("end");
    });
</script>

Is it someting to do with firefox or the way I am loading the editor? Note - the editor works on my form very nicely for submitting and posting data.


Solution

  • You are trying to call getContent() before the editor is fully initialized. The init() method is asynchronous and I would suspect your alert is running before the editor is fully initialized.

    The easiest way to make sure that you are waiting until the editor is fully initialized is to rely on the init event that the editor provides:

    tinymce.init({
      selector: '#myTextarea',
      ...
      ...
      setup: function (editor) {
        editor.on('init', function () {
          // This is a common function for loading initial content into TinyMCE 
          editor.setContent('Using the on init stuff!');
          // You could run your alert here as you know the editor in initialized
          alert(tinymce.get('mytextarea').getContent());
        });
      }
    });