jQuery – Word and Character Counter

Publicités

A small script to count the number of words and characters in a input or textarea field

<input id="text" name="text" type="text">
<span id="count"></span>";

We place the script in jQuery's function ready to make sure the HTML has been fully loaded.

<script></script>
   (document).ready (function)
      count ($("#text"),$("#count")
      $("#text").
         count ($("#text"),$("#count")
      });
   });

function count (src,dest)
  var txtVal - src.val();
  var words - txtVal.trim().replace(/s/gi, ').split(').length;
  var tanks - txtVal.length;
  if (tanks)
  dest.html (words' words<br>'Tanks' tanks');
}


Publicités

Leave a Reply