I try to cut a too long text and replace it with "...".
HTML:
<div class="test">
<span>Item text goes in here but it is way too long to fit inside a select option that has a fixed width adding more</span>
</div>
Jquery:
$.each($('.test span'), function(key, testName) {
var curText = $(testName).text();
$(this).attr('title', curText);
var lengthToShortenTo = Math.round(parseInt($(this).parent('.test').css('width'), 10) / 7.3);
if (curText.length > lengthToShortenTo) {
$(this).text(curText.substring((curText.length - lengthToShortenTo), curText.length) + '... ');
}
});
The output of the shortened span is:
select option that has a fixed width adding more...
But I want to get the first part of the text and not the last. So this is what I want too get:
Item text goes in here but it is way too long...