Skip to content

Instantly share code, notes, and snippets.

@slimeygecko
Forked from tamewhale/word-count.js
Last active August 29, 2015 14:04
Show Gist options
  • Save slimeygecko/6b35bc94199745176e24 to your computer and use it in GitHub Desktop.
Save slimeygecko/6b35bc94199745176e24 to your computer and use it in GitHub Desktop.
// this assumes an existing html tag containing the upper limit
// of the word count, with an id of #word-count- + the id of the tinymce instance
// e.g. <p>Words left: <span id="word-count-textarea-1">200</span></p>
// add as a parameter to the tinyMCE.init({}); function
setup: function(ed) {
var text = '';
var span = document.getElementById('word-count-' + ed.id);
if(span) {
var wordlimit = span.innerHTML;
ed.onKeyDown.add(function(e) {
text = ed.getContent().replace(/(< ([^>]+)<)/g, '').replace(/\s+/g, ' ');
text = text.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
wordcount = wordlimit - (text.split(' ').length);
span.innerHTML = wordcount;
if(wordcount <= 0 && e.keyCode != 8) {
return tinymce.dom.Event.cancel(e);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment