Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  194] [ 1]  / answers: 1 / hits: 132970  / 14 Years ago, sun, january 9, 2011, 12:00:00

I have long titles and want truncate them but in a way that no words break, I mean the cutting happen between words not cutting a word.



How can I do it using jquery?


More From » jquery

 Answers
3

From:
jQuery text truncation (read more style)



Try this:



var title = This is your title;

var shortText = jQuery.trim(title).substring(0, 10)
.split( ).slice(0, -1).join( ) + ...;




And you can also use a plugin:





As a extension of String



String.prototype.trimToLength = function(m) {
return (this.length > m)
? jQuery.trim(this).substring(0, m).split( ).slice(0, -1).join( ) + ...
: this;
};


Use as



This is your title.trimToLength(10);

[#94316] Thursday, January 6, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kyrona

Total Points: 422
Total Questions: 111
Total Answers: 97

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;