Count Words : String « Data Type « JavaScript DHTML






Count Words

  
<HTML>
<HEAD>
<SCRIPT>
function countWords(str){
   var count = 0;
   words = str.split(" "); 
    for (i=0 ; i < words.length ; i++){
       if (words[i] != "")
          count += 1; 
    }
    document.theForm.results.value = count;
}

</SCRIPT>
</HEAD>
<BODY>
<FORM name="theForm">
Enter a text string:
<TEXTAREA name=inStr rows=5 cols=90>
</TEXTAREA>
<INPUT type=button value="Count Words" onClick="countWords(document.theForm.inStr.value)";>
<INPUT type=button value="Clear" onClick='document.theForm.inStr.value=""';>
<TEXTAREA name=results rows=5 cols=90>
</TEXTAREA>
</FORM>  
</BODY>
</HTML>

   
    
  








Related examples in the same category

1.Get char at
2.String big
3.String blink
4.Make string bold
5.Create anchor from a string
6.Get char Code
7.Concat String
8.String escape
9.Escape double quote
10.Including a special character in a string
11.String font Color
12.String font Size()
13.String index Of
14.Combine indexOf and substring methods to get the anchor part of a url
15.Search the string and counts the number of e's
16.String from Char Code
17.String in italics
18.String last Index Of
19.String length
20.Compare string in locale format
21.Make String link
22.Match string
23.String replace
24.Slice a string
25.Get small String
26.Split a string
27.Return strike String
28.String sub() Example
29.Get sub string (substr())
30.Use substring method to get the sub string
31.Get super script (sup)
32.Get Locale Lower Case string (toLocaleLowerCase)
33.Get Locale Upper Case string (toLocaleUpperCase)
34.Get Lower case String (toLowerCase)
35.Get Upper Case string (toUpperCase)
36.'unescape()' Example
37.Check the first letter of a name
38.Use String.indexOf method to validate an email address
39.Validate a time value input with string.indexOf method
40.Return string value from a function
41.Tab string character
42.Adding trim function to String
43.Double quote string character
44.Single quote string character
45.Back slash string character
46.Backspace string character
47.Capitalize Words