benji
benji

Reputation: 681

Cut down a string in text area within particular limit in jquery

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

Answers (5)

Starx
Starx

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));
  }
});

Demo

Upvotes: 0

sandeep
sandeep

Reputation: 2234

$('#btn').click(function(){

    $('#test').val($('#test').val().slice(0, 100));

});

check demo

http://jsfiddle.net/ycDfb/

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76910

You should do something like

$('textarea').keyup(function(){
  if(this.value.length > 100){
    this.value = this.value.substring(0, 100);
  }
}) 

Upvotes: 0

Andreas Wong
Andreas Wong

Reputation: 60594

$('#textarea').bind('keyup keydown blur', function() {
   $(this).val($(this).val().slice(0, 100));
})

Upvotes: 0

Duke
Duke

Reputation: 37070

simply use this

$('#textarea').bind('keyup', function() {
    $('#textarea').val($('#textarea').val().slice(0, 100));
});

Upvotes: 6

Related Questions