Search code examples
javascriptjqueryhtmltrim

JavaScript to trim after n characters


I have a HTML page which shows a Name dynamically. Now this name (FN LN) can be upto 240 charcaters. But at the UI side, I want to trim the FN/LN after about 50 characters and replace with ...

How can I do this using Javascript/jQuery


Solution

  • $("#FN, #LN").each (function () {
      if ($(this).text().length > 50)
        $(this).text($(this).text().substring(0,50) + '...');
    });
    

    This should work.