Reputation: 681
I have a text area in my user description form . I need to cut down the string , if user enter more than 100 chars. I want to use jquery for this
Upvotes: 2
Views: 2451
Reputation: 79049
For better result restrict the text or keypress
event
var maxchar = 100;
$('textarea').keypress(function(e){
if($(this).val().length > maxchar){
$(this).val($(this).val().substring(0, maxchar));
}
});
Upvotes: 0
Reputation: 2234
$('#btn').click(function(){
$('#test').val($('#test').val().slice(0, 100));
});
check demo
Upvotes: 0
Reputation: 76910
You should do something like
$('textarea').keyup(function(){
if(this.value.length > 100){
this.value = this.value.substring(0, 100);
}
})
Upvotes: 0
Reputation: 60594
$('#textarea').bind('keyup keydown blur', function() {
$(this).val($(this).val().slice(0, 100));
})
Upvotes: 0
Reputation: 37070
simply use this
$('#textarea').bind('keyup', function() {
$('#textarea').val($('#textarea').val().slice(0, 100));
});
Upvotes: 6