Skip to content

Instantly share code, notes, and snippets.

@reneviering
Created December 1, 2015 13:45
Show Gist options
  • Save reneviering/78ed86bbbfb5e77d4c27 to your computer and use it in GitHub Desktop.
Save reneviering/78ed86bbbfb5e77d4c27 to your computer and use it in GitHub Desktop.
Simple snippet to implement read more functionality.
function shorten(options) {
var item = $(options.selector);
var sourceText = $(item).text().trim();
var firstSection = sourceText.substring(0, options.textLength);
var lastSection = sourceText.substring(options.textLength, sourceText.length);
var firstItem = $('<span>');
firstItem.text(firstSection);
var readMoreItem = $('<a>');
readMoreItem.attr('href', '#read-more');
readMoreItem.addClass('readMore');
readMoreItem.text(options.readMoreText);
$(firstItem).append(readMoreItem);
$(item).html(firstItem);
$(readMoreItem).off('click');
$(readMoreItem).on('click', function () {
$(item).text(sourceText);
});
}
// Invoke it
shorten({
selector: '.shortenPlease',
textLength: 40,
readMoreText:' ...read more'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment