Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

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

You are here: irt.org | FAQ | JavaScript | Form | Q1621 [ previous next ]

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>

©2018 Martin Webb