Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  196] [ 3]  / answers: 1 / hits: 30493  / 11 Years ago, thu, march 14, 2013, 12:00:00

I want to be able to display a string of characters up to 10 characters. If the string goes over 10 characters, I'd like to append '...' to the end.



For example, if I have the string:



'helloworldmynameisryan'


I want it to be displayed like so:



'helloworld...'


I'm just displaying my string in a div like this:



<div>DisplayMessage</div>


Is there a class that I could create that would only apply if the string were over 10 char?


More From » html

 Answers
8

Unfortunately there isn't a good cross browser way to do this using only CSS. 'text-overflow' relies on the width of the string, and not the length of string as you need.



You can use the .length property of strings in javascript to achieve this



function ellipsify (str) {
if (str.length > 10) {
return (str.substring(0, 10) + ...);
}
else {
return str;
}
}


Hope this helps.


[#79610] Tuesday, March 12, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;