4

Is there any way by which I can count the characters entered in tinymce editor. I just want to set minimum character requirement. Or how to use tinyMCE.activeEditor.getContent() properly for this purpose.

bɪˈɡɪnə
  • 1,087
  • 2
  • 23
  • 46

2 Answers2

7

I believe that the better way to do this is using the getContent api:

// Get the HTML contents of the currently active editor
tinymce.activeEditor.getContent().length

// Get the raw text of the currently active editor
tinymce.activeEditor.getContent({format: 'text'}).length;

// Get content of a specific editor:
tinymce.get('content id').getContent().length

Please note the usage of the format parameter in the second example in order to get the raw text instead of the parsed text with HTML tags.

roperzh
  • 890
  • 7
  • 12
  • A perfect solution for those on version 4 where the wordcount plugin it's still limited to only words (and you don't have the option to upgrade of course). – elvin Sep 08 '20 at 19:51
0

Try this ->

 var body = tinymce.get("txtTinyMCE").getBody();
 var content = tinymce.trim(body.innerText || body.textContent);
 return content.length;

See -> http://www.aspsnippets.com/Articles/Character-Count-and-Character-Limit-validation-for-TinyMCE-editor-using-JavaScript.aspx

ahervin
  • 461
  • 2
  • 15