$('textarea').bind('paste keyup blur', function() { $(this).val(function(i, val) { return val.substr(0, 5); }); });
jsFiddle .
Update
I don't know why, but it prints function(i, val) { return val.substr(0, 5); } function(i, val) { return val.substr(0, 5); } in the text area every time.
It looks like you are using an older version of jQuery (pre 1.4 ). The recovered code below will work.
$('textarea').bind('paste keyup blur', function() { $(this).val(this.value.substr(0, 5)); });
jsFiddle .
The previous code would not work until jQuery 1.4, because it only expected the string as an argument to val() . Passing a function, its toString() was implicitly called, returning a string representation of the function.
source share