Count the number of words with JavaScript

by admin on July 24, 2009 · 0 comments

Sometimes you need a quick way to count the number of words in a string. Suppose you've typed out a blog post or essay and you want to ensure the minimum word count. Well, you could past into Word or OpenOffice, but this is much quicker! This handy little code snippet provides a textarea and the inline JavaScript to display the number of words as you type.

<form name="form_count">
<textarea rows="8" name="word_input" cols="40" wrap="virtual" onkeyup="f=document.form_count;f.word_count.value=f.word_input.value.split(' ').length;"></textarea>
<p><input type="button" value="Count Words" onClick="f=document.form_count;f.word_count.value=f.word_input.value.split(' ').length;">
<input type="text" name="word_count" size="20"></p>
</form>

Here's what it looks like:

Previous post:

Next post: