renat natanael renat natanael - 1 year ago 131
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

Answer Source

You can:

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

2) or use maxlength attribute (HTML5).

<textarea maxlength="10"></textarea>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download