Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set character limit to display in a paragraph

Tags:

json

jquery

I am pulling from a JSON feed and I just want to display a maximum of 10 characters of the string and then do a ... afterwards. How do I do that with JQuery?

like image 868
meijiOrO Avatar asked Nov 08 '10 17:11

meijiOrO


2 Answers

You can use CSS to set up an ellipsis:

.myparagraph {
   white-space: nowrap;
   width: 10em;
   overflow: hidden;
   text-overflow: ellipsis;
}

Then there's no need for any jQuery or other coding.

References:

  • http://www.quirksmode.org/css/textoverflow.html
  • http://www.electrictoolbox.com/ellipsis-html-css/

(note that first link - Quirksmode.org is an excellent resource generally for CSS and Javascript stuff)

like image 114
Spudley Avatar answered Oct 26 '22 14:10

Spudley


I haven't checked this for off by one errors, so you might have to adjust for poor indexing.

var txt = SomeStringFromFeed;
if(txt.length > 10)
{
  txt = txt.substr(0,10) + "...";
}
return txt;
like image 45
Thomas Langston Avatar answered Oct 26 '22 14:10

Thomas Langston