Created
December 1, 2015 13:45
-
-
Save reneviering/78ed86bbbfb5e77d4c27 to your computer and use it in GitHub Desktop.
Simple snippet to implement read more functionality.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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