substr method on link text and add ellipses?
Question
$("a.newslinks").each(function(){
if ($(this).text().length > 38) {
$(this).text().substr(35); //does not work
$(this).append('...'); //works
$(this).css({ "color" : "#ff00cc" }); //works
}
});
If a link has its text longer than 38 characters, how can I trim it to 35 chars and add an elipses at the end?
Solution
substr(35)
will chop 35 characters off the start of the string - not limit it to 35 chars in length.
Try:
.substr(0, 35)
Also, this function just returns a new string - it doesn't change the original. So you need to do
$(this).text($(this).text().substr(0, 35));
OTHER TIPS
Try:
$(this).text($(this).text().substr(0, 35));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow