|
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>
|
|
|
Copyright © 1996-2008 irt.org, All Rights Reserved.