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.

No comments:

Post a Comment