Find the number of words in a string using the JavaScript split()
method.
<!DOCTYPE html> <html lang="en"> <head> <title>jQuery Count Number of Words in a String</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var words = $.trim($("textarea").val()).split(" "); document.getElementById("demo").innerHTML = words.length; });/*w w w .ja v a 2 s . co m*/ }); </script> </head> <body> <p id="demo"></p> <textarea cols="50">The quick brown fox jumps over the lazy dog.</textarea> <br> <button type="button">Count Words</button> </body> </html>