TinyMCE Keyup Function

I am trying to run a function on Keyup of the TinyMCE Editor on new post and edit post screens.

I’m having a hard time catching the keyboard action when a user types something.

jQuery(document).ready(function() {
                setTimeout(function(){
                    var content = "tinymce";
                    var tinyMCEcontent = tinymce.editors.content.getContent();
                    console.log(tinyMCEcontent.length);
                },3000);
            });

This was my attempt at getting the content of the TinyMCE editor. I am able to see the length. How can I run this function on Keyup? I have tried something like this:

jQuery(document).ready(function() {
          jQuery("#tinymce").keyup(function() {
                setTimeout(function(){
                    var content = "tinymce";
                    var tinyMCEcontent = tinymce.editors.content.getContent();
                    console.log(tinyMCEcontent.length);
                },3000);
          });
 });

But it isn’t catching the keyup at all.

What I am trying to do is set a limit on the post type content.

2 Answers
2

// get tinymce instance of wp default editor
var ed = tinyMCE.getInstanceById('content');

// add a callback to tinyMCE.onKeyUp event
ed.onKeyUp.add(function(){
    // format: text will strip html tags
    console.log( ed.getContent({ format: 'text' }) ).length;
})

http://www.tinymce.com/wiki.php/api3:event.tinymce.editor.onkeyup

Leave a Comment