renat natanael renat natanael - 3 months ago 11
jQuery Question

jQuery Counter: Stop type when reach limit

I create a counter for text area and for now I success to count the length, but I want after it reach the limit, user can't type anymore.

How to make it stop able to type when reach the limit?

$('textarea').keyup(function () {
var max = 10;
var len = $(this).val().length;
var char = max - len;
if (len >= max) {
$(this).parent().find('.counter').text(char + '/'+max);
else {
$(this).parent().find('.counter').text(char + '/'+max);

heres the jsfiddle


You can:

1) change keyup to keydown and return false;. Fiddle:

2) or use maxlength attribute (HTML5).

<textarea maxlength="10"></textarea>