Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1621 How do I get a word count of the text in a textarea?

irt.org | Knowledge Base | JavaScript | Form | Q1621 [ previous next ]

Q1621 How do I get a word count of the text in a textarea?

Try:

<script language="JavaScript"><!--
function wordcount(string) {
  var a = string.split(/\s+/g); // split the sentence into an array of words
  return a.length;
}
//--></script>

<form name="myForm">
<textarea name="myText" rows="3" cols="40">
This is some sample text. There is punctuation (sprinkled)
about, plus some line-separators. The answer should be 19.
</textarea>
<input type="button" value="Word Count" onClick="alert(wordcount(this.form.myText.value))">
</form>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.