Created
July 31, 2015 06:58
-
-
Save DanWebb/a2861fec5a3cbd2ab725 to your computer and use it in GitHub Desktop.
Add a read more link to any text block
This file contains hidden or 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
$.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