I am writing this code which is to count the amount of characters remaining in a textarea. At first it seemed very simple and has enough documentation on the internet. But I realized that when I copy and paste some text into the textarea, the total remaining characters are positive, meaning I can still type. But this is not the case. Besides not allowing to enter more characters, even I deleting some of the characters with the backspace I can not type anymore. For example, if the text ends in "ball" I can not type "ball" and if I delete it for "bol" (by removing the "a") I can no longer write "ball".
To be clear, the code below works under normal conditions of temperature and pressure, but when a "paste" occurs it gets anomalous behavior, which is to display the remaining number of characters, for example 200, and does not allow more insertion of data. Here is the code I did:
$("#textAreaComplemento").bind("keyup change", function (e) {
calculaCaracteresRestantes();
});
function calculaCaracteresRestantes() {
if ($('#textAreaComplemento').val() == undefined) { return false; }
var text_length = $('#textAreaComplemento').val().length;
var text_remaining = text_max - text_length;
$('#textarea_feedback').html(text_remaining);
return true;
}
Any suggestion of what may be will be welcome.
EDIT : To reproduce, just copy the code that is in the html comment of this fiddle link . Paste the code as far as you can into the textarea and then try typing.