Hi,
Please help me to count number of words in a textarea using jquery.
Example:
how to count number of words
It should be counted as 6.
Hi makenzi.exc,
Split the textarea value with space for calculating the count of words.
Refer below sample.
HTML
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } </style> </head> <body> <textarea id="txtMessage" rows="5" cols="20"></textarea> <br /> <span id="lblCount"></span> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <script type="text/javascript"> $(function () { $("#txtMessage").keyup(function () { var wordCount = 0; var words = $("#txtMessage").val().split(" "); wordCount = words.filter(function (word) { return word.length > 0; }).length; $("#lblCount").html(wordCount); }); }); </script> </body> </html>
Demo
© COPYRIGHT 2025 ASPSnippets.com ALL RIGHTS RESERVED.