Skip to content

Instantly share code, notes, and snippets.

@DanWebb
Created July 31, 2015 06:58
Show Gist options
  • Save DanWebb/a2861fec5a3cbd2ab725 to your computer and use it in GitHub Desktop.
Save DanWebb/a2861fec5a3cbd2ab725 to your computer and use it in GitHub Desktop.
Add a read more link to any text block
$.fn.extend({
readMore: function(truncate) {
return this.each(function() {
var $self = $(this);
var originalText = $self.text();
if(originalText.length<=truncate) return;
$self.text($.trim(originalText).substring(0, truncate));
$self.append('<span class="read-more">...<a href="#">Read more</a></span>');
$self.find('a').click(function(e) {
e.preventDefault();
$(this).parents('span').after(originalText.substring(truncate, originalText.length));
$(this).parents('span').remove();
});
});
}
});
// example usage
$(function() {
// read more on product description, truncate from 90 characters
$('.product-description p').readMore(90);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment