Tuesday, October 28, 2014

How To Limit Characters in Div With jQuery?

It is really easy to limit the number of characters in a div, particular class, ID etc by a particular value.
$(function(){

  $(".target-div").each(function(i){

    var len = $(this).text().length;

    if(len > 100){

      $(this).text($(this).text().substr(0,100)+'...');

    }
  });
});
If desired, remove the dots from the end ('...'). I would recommend keeping it to let your users know that the text was shortened.

2 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from JQuery Training in Chennai . or learn thru JQuery Training . or learn thru ES6 Online Training. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from JQuery Training in Chennai . or learn thru JQuery Training . or learn thru ES6 Online Training. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete