Skip to content

Instantly share code, notes, and snippets.

@bgmort
Last active December 10, 2015 20:38
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 bgmort/4489877 to your computer and use it in GitHub Desktop.
Save bgmort/4489877 to your computer and use it in GitHub Desktop.
Simple jquery function to trim a line of text with ellipses on left side, like text-overflow: ellipsis clip will in Firefox only. Changes the text permanently, so the amount of text hidden will not change when the window is resized. For this to work, the element you pass in needs to have overflow: hidden and white-space: nowrap; http://stackover…
/**
* Simple jquery function to trim a line of text with ellipses on left side,
* like text-overflow: ellipsis clip will in Firefox only. Changes the text
* permanently, so the amount of text hidden will not change when the window
* is resized.
* For this to work, the element you pass in needs to have overflow: hidden
* and white-space: nowrap;
*
* http://stackoverflow.com/questions/9793473/text-overflow-ellipsis-on-left-side
*/
$.fn.trimLeft = (function(){
var trimContents = function(row, node){
while (row.scrollWidth > row.offsetWidth) {
var childNode = node.firstChild;
if (!childNode)
return true;
if (childNode.nodeType == document.TEXT_NODE){
trimText(row, node, childNode);
}
else {
var empty = trimContents(row, childNode);
if (empty){
node.removeChild(childNode);
}
}
}
}
var trimText = function(row, node, textNode){
var value = '...' + textNode.nodeValue;
do {
value = '...' + value.substr(4);
textNode.nodeValue = value;
if (value == '...'){
node.removeChild(textNode);
return;
}
}
while (row.scrollWidth > row.offsetWidth);
}
return function(row){
this.each(function(i, row){
trimContents(row, row);
});
return this;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment