1

My main goal is to get editor content when it changes and update a live preview. For this I need to add an event listener to the editor. BTW, I have two editor in the same page and the page is loaded via ajax.

Now back to my problem. I can't get to the instance of the tinyMCE editors. I can console.log() them but get empty result when I try to stringify them.

Below is the result of console.log(tinymce.editors).

console.log(tinymce.editors)

So, whenever I try to access editors using tinymce.editors.contentleft, tinymce.editors['contentleft'], tinymce.get('contentleft') or tinymce.EditorManager.get('contentleft') in all these cases I get undefined or null

Working more then couple days on this. Would appreciate a proper direction soon.

5

If you wait for the SetupEditor event then you can access the editors:

add_action( 'admin_print_footer_scripts', function () {
    ?>
    <script type="text/javascript">
    jQuery(function ($) {
        if (typeof tinymce !== 'undefined') {
            tinymce.on('SetupEditor', function (editor) {
                if (editor.id === 'contentLeft') {
                    // Could use new 'input' event instead.
                    editor.on('change keyup paste', function (event) {
                        console.log('content=%s', this.getContent());
                    });
                }
            });
        }
    });
    </script>
    <?php
} );
  • spot on! So, I guess I was running the script early? – Sisir Jun 29 '15 at 10:07
  • Yes! (tinymce takes several microseconds to setup each editor so the fact that yours is a secondary one probably exasperated the situation.) – bonger Jun 29 '15 at 10:22
  • 1
    ps: we should probably add a typeof tinymce != 'undefined' check, to avoid error on pages where tinymce isn't defined. – birgire Aug 27 '15 at 9:10
  • Good point @birgire ta, I'll update the answer... – bonger Aug 27 '15 at 10:02
  • I think function(editor) should be function(event) { var editor = event.editor; .. – Nathan Moinvaziri Oct 6 '18 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.