We can count the number of words by using split method of javascript.
What split method does:
split() - Splits a string into an array of substrings
Below is an example:-
<form name=countingWords>
<textarea rows="12" name="countingWords1 cols="38" wrap="virtual"></textarea><br>
<input type="button" value="Calculate Words" onClick="quickcount()"> <input type="text" name="countingWords3" size="20">
</form>
<script type="text/javascript">
function quickcount(){
var displaycontent=document. countingWords.countingWords1.value
displaycontent = displaycontent.split(" ")
document.countingWords.countingWords3.value= displaycontent.length
}
</script>
By using split() method, we separate the text by spaces and then counting the words by length method. Without the help of split method counting the number of words would be a difficult task.
0 Comment(s)