[thelist] Word counter

Simon Willison cs1spw at bath.ac.uk
Sun Oct 12 11:18:07 CDT 2003


Mark Jones wrote:
> I am looking for a routine on a form that will count the number of 
> charaters being typed into that form.
> 
> Any ideas?

Here's a quick-and-dirty solution:

<script type="text/javascript">
function countMe(obj, targetID) {
     var target = document.getElementById(targetID);
     target.innerHTML = 'Count: ' + obj.value.length;
}
</script>

<form>
<textarea name="text" id="myText" onkeyup="countMe(this, 
'counter')"></textarea>
<div id="counter">Count: 0</div>
</form>

<tip type="Rapid prototyping with HTML">
If you want to try out a snippet of HTML, CSS or Javascript quickly
without creating and saving a new file (for example to try out a code
snippet before posting it to the list) use Jesse Ruderman's Real-time
HTML editor:

http://www.squarefree.com/htmledit/
</tip>

Hope that helps,

Simon Willison
http://simon.incutio.com/



More information about the thelist mailing list