This is my solution for you:
function text_counter(input_text, target) { var max = input_text.attr("maxlength"); input_text.keyup(function() { target.text(this.value.length + "/" + max) }); } text_counter($("#description"), $("#description_counter"));
Example: http://jsfiddle.net/jonathon/dkWHp/
A couple of differences. Firstly, I use the keyup event. Otherwise you're running the code before the character is removed (maybe that's why you're using the setTimeout) - it also gets fired on a backspace. I also pass the jQuery objects into the text_counter function. This was you just create the single jQuery selector (instead of the multiple $(input_text) calls). In the keyup handler, I just call this.value.length since this is a HTML input element and I don't need to bother jQuery for this.
For good measure, here's another implementation of text_counter:
function text_counter(input_text, target) { var max = input_text.attr("maxlength"); setInterval(function(){ target.text(input_text.val().length + "/" + max) }, 100); }
Example: http://jsfiddle.net/jonathon/vZHGU/
Instead of handling the key events, I just set up a timer which sets the text of target every 100ms. Why show this? When you handle a key event, it will only fire once. The second piece of code I gave will continually poll the length of the text box so that the span gets updated even if the backspace button is held down.
I don't like the second solution because it creates unnecessary work (the setInterval will run regardless of the user interaction with the box) but I do show it because you could play with both solutions. For example, you could invoke 5 seconds of polling on a keypress or something to get the best of both worlds :)