I am trying to create a comment on a form blog with textarea and span that shows the user the number of remaining characters that can be entered into the text area.
So, I have this form:
<form action="comment.php" method="POST" accept-charset="utf-8"> <textarea name="comment" id="comment" rows="4" cols="56"></textarea> <input type="submit" value="Post" /> <span id="comment-chars-left">512</span> characters left </form>
And then I wrote the following jQuery code:
$('#comment') .keydown(function(event) { $('#comment-chars-left').html('' + (512 - $textarea.val().length)); });
The problem is that the first time .keydown call .keydown , the number of remaining characters is called, and then the new character is entered into textarea . Thus, the number of remaining characters does not have the correct value, being one more. To make this work .keydown must be called after inserting a new character.
How can I solve this problem?
source share