Skip to content

Instantly share code, notes, and snippets.

@ricardodsanchez
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save ricardodsanchez/11245192 to your computer and use it in GitHub Desktop.

Select an option

Save ricardodsanchez/11245192 to your computer and use it in GitHub Desktop.
Count the number of words in a text editor, text box or text area and display total number of words as you type
$('#editor').keyup(function () {
typewatch(function () {
// executed only 500 ms after the last keyup event.
var words = $('#editor').html().toString().split(' ');
// get number of words
// add the value of words.length to a label, or other html element for displaying purposes as shown below
$('.word-count').text(words.length);
}, 500);
});
var typewatch = (function () {
var timer = 0;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment