I have the following function that counts the number of characters left the user has to type in a textarea:
$("textarea").keyup(function() {
var max = 500;
var length = $(this).val().length;
var char = max - length;
if (length > max) {
$(this).parent().find('.counter span').html('<div class="text-error">' + char + '</div>');
} else {
$(this).parent().find('.counter span').text(char);
}
});
If I introduce a new DOM to the page, the above function doesn't work for the newly introduced textarea. How can I make it work with new DOMs?
Thanks.