I have asked a similar question previously, but didn't give enough context. As a result I received an excellent, technically-correct answer that didn't solve my issue.
I've also looked around on Stack but don't know enough about jQuery to find my answer.
I need to truncate multi-line text with jQuery. The code needs to add/remove text as well when the browser window expands and contracts. So from my minimal understanding the code needs to store the text before truncating it so that it can add text back in when the browser window is expanded.
Initially this piece of code solved my problem:
$(function () { var initial = $('.js-text').text(); $('.js-text').text(initial); while($('.js-text').outerHeight() > $('.js-text-truncator').height()) { $('.js-text').text(function(index, text) { return text.replace(/\W*\s(\S)*$/, '...'); }); } $(window).resize(function() { $('.js-text').text(initial); while($('.js-text').outerHeight() > $('.js-text-truncator').height()) { $('.js-text').text(function(index, text) { return text.replace(/\W*\s(\S)*$/, '...'); }); } }); }); This code no longer cuts it as when I use these .js classes more than once on a single page all the text is stored together and then spat out whenever the classes are being used.
Here is a jsFiddle of the the issue:
I need to store each .js-text text separately, so that I can use this jQuery snippet across a large project and have all instances of truncated text fed back into the DOM if a user were to expand their browser window size.
Is this possible? If so, how would I do it?
Thanks in advance for tackling my question. I hope I have been specific enough in what I'm looking for.
text-overflow. Have a look here for example: css-tricks.com/line-clampin.