March 14, 2012

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

Question by Benedict Peter

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

Answer by duke

simply use this

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

Answer by Starx

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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!