arozwalak
2/13/2015 - 12:49 PM

Javascript: show/hide more text

Javascript: show/hide more text

function shortenText (textElement, max) {
    var text = textElement.text();

    if (text.length > max) {
        var shortText = text.substr(0, max);
        if (shortText.substr(shortText.length - 1) == ' ') {
            shortText = shortText.substr(0, max - 1);
        }
        shortText += '...';

        textElement.after('<div class="fullText is-hidden">' + text + '</div>');
        textElement.text(shortText);
    }
}

function showFullText(el, fullText) {
    el.text(fullText.text());
    fullText.remove();
}