Skip to content

Instantly share code, notes, and snippets.

@kmcconnell
Created May 17, 2017 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmcconnell/1e4772bf02c5c0f8f3b47712db573749 to your computer and use it in GitHub Desktop.
Save kmcconnell/1e4772bf02c5c0f8f3b47712db573749 to your computer and use it in GitHub Desktop.
Truncate long text within a span.
/**
* Truncate long text within a span. Hover over span to see full text.
*
* @param {string} data The text content to include between span elements if longer
* than the specified maximum characters.
* @param {integer} chars The maximum number of characters before truncating with an
* ellipse.
*
* @return {string} The ellipsed span with the full text set as the title attribute if
* the specified data is greater than the max characters, otherwise
* the data is returned unaffected.
*/
function ellipse_span(data, chars = 50)
{
return data.length > chars ?
"<span title=\""+data+"\">"+data.substr(0,chars)+"...</span>"
: data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment