Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 421
copenndthagen Avatar asked Dec 08 '11 06:12

copenndthagen


People also ask

How do you remove all characters from a string after a specific character in JavaScript?

Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str.

How do you trim a character in JavaScript?

TrimLeft(), used to remove characters from the beginning of a string. TrimRight(), used to remove characters from the string's end. Trim(), used to remove characters from both ends. JavaScript's native functions, like those of many other languages, solely remove whitespace characters.

Can we use \n in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

How do I limit the length of a string in JavaScript?

To limit the string length, assign an integer number to the maxLength option. By default, the string length is unlimited.


1 Answers

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

This should work.

like image 113
Jake Feasel Avatar answered Oct 17 '22 03:10

Jake Feasel