Well, you probably need to consider ways to change focus. Do you want to clear the counter when the field is again focused or not? You must also decide whether you are actually counting the characters in the field, or if you want the actual discrete keystrokes — pressing the shift key, for example, will not add any characters, but that key is pressed.
In any case, perhaps it will be something like this:
$(function() { var keyCount = 0; $('#q').keyup(function() {
If you consider the event "keypress" instead of "keyup", you may need to calculate the actual length of the text field value, rather than trying to count the events.
source share