Keeping in mind what Etienne Martin says, you can use oninput
, as it detects any change within texarea. Detect if you copy and paste text.
$('#textarea').on('input', function() {
var max = 400;
var len = $(this).val().length;
var char = max - len;
if (len >= max) {
$('#charNum').text(' You have reached the character limit.');
$('#charNum').addClass("text-danger"); // optional, adding a class using bootstrap
} else if (char <= 10) {
$('#charNum').text(char + ' You are reaching the character limit.');
$('#charNum').addClass("text-warning"); // optional, adding a class using bootstrap
} else {
var char = max - len;
$('#charNum').text(char + ' characters remaining.');
$('#charNum').addClass("text-success"); // optional, adding a class using bootstrap
}
});