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
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
if ($('#name').text().length > 50)
{
$('#name').text( $('#name').text().substring(0,50)+"..." );
}
But you can also use CSS for this: http://mattsnider.com/css/css-string-truncation-with-ellipsis/
here's a regex way to do that :
text.replace(/(^.{50}).*$/,'$1...');
Since you've asked, if you want to jqueryfy this functionality, you can make a plugin out of it :
$.fn.trimAfter(n,replacement){
replacement = replacement || "...";
return this.each(function(i,el){
$(el).text($(el).text().substring(n) + replacement);
});
}
And use it like this :
$("#FN, #LN").trimAfter(50,'...');