Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery Count Number of Words in a String</title> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var words = $.trim($("textarea").val()).split(" "); alert(words.length); }); }); </script> </head> <body> <textarea cols="50">The quick brown fox jumps over the lazy dog.</textarea> <br> <button type="button">Count Words</button> </body> </html>