September 12th, 2006
Count the number of words with JavaScript
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>
<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:

on March 2nd, 2007 at 1:04 pm
on March 2nd, 2007 at 1:06 pm
hey explain this to me, how do i set this up on a website?
on March 2nd, 2007 at 6:04 pm
well ..this works fine only if i give spaces between two words but doesn't work when two words are comma separated..
on March 2nd, 2007 at 7:56 pm
then just make the delimiter the comma character instead of a space