2

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.

1
// 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

| improve this answer | |
1

For WordPress 3.9+ running tinymce 4:

    window.onload = function () {
        tinymce.get('content').on('keyup',function(e){
            console.log(this.getContent().replace(/(<[a-zA-Z\/][^<>]*>|\[([^\]]+)\])|(\s+)/ig,'').length);
        });
    }
| improve this answer | |

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.