Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery function for multiline ellipsis text

Tags:

html

jquery

css

I've created a fiddle here: http://jsfiddle.net/mupersan82/JYuSY/

Multiline ellipsis function:

$(function() {
var ellipsisText = $('.multilineEllipseText');
var textContainer = $('.incidentCellBottomRight').height();
while ($(ellipsisText).outerHeight(true) > textContainer) {
    $(ellipsisText).text(function(index, text) {
        return text.replace(/\W*\s(\S)*$/, '...');
    });
}

});

The function is taken from here: Cross browsers mult-lines text overflow with ellipsis appended within a width&height fixed div?

The function sort of works for multiline content but doesn't allow the content to extend to the edge. With singleline content, the ellipsis is added after only one word.

It should allow text up to the max height of its parent div, which is 100px.

like image 983
mupersan82 Avatar asked Mar 31 '26 15:03

mupersan82


1 Answers

In your code you have:

var $ellipsisText = $('.multilineEllipseText');

this selects all .multilineEllipseText elements. Yet you are using .outerHeight(true) later on, which will return the height of the first element in the given set only.

So it works for the first element, but for each element after that, you're comparing to the wrong height (that from the first).


Here is how you can fix that:

var textContainerHeight= $('.incidentCellBottomRight').height();

$('.multilineEllipseText').each(function () {
  var $ellipsisText = $(this);

  while ($ellipsisText.outerHeight(true) > textContainerHeight) {
    $ellipsisText.text(function(index, text) {
      return text.replace(/\W*\s(\S)*$/, '...');
    });
  }
});

demo: http://jsfiddle.net/JYuSY/1/

like image 168
Yoshi Avatar answered Apr 02 '26 22:04

Yoshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!