Skip to content

Instantly share code, notes, and snippets.

@jenhuls
Last active March 17, 2022 17:17
Show Gist options
  • Save jenhuls/5586d7ac2a017c4edb16a389749b5b5a to your computer and use it in GitHub Desktop.
Save jenhuls/5586d7ac2a017c4edb16a389749b5b5a to your computer and use it in GitHub Desktop.
[jQuery Read More] #jquery
.description .more--content span {
display: none;
}
.description .more--link {
display: block;
}
(function($) {
// Usage:
/*
<div class="description">
<p class="more-text">
<?php echo $content ?>
</p>
</div>
*/
jQuery(function() {
// Configure/customize these variables.
var showChar = 50; // How many characters are shown by default
var ellipsestext = "...";
var moretext = "Read more";
var lesstext = "Read less";
$('.description p').each(function() {
var content = $(this).html();
if(content.length > showChar) {
var c = content.substr(0, showChar);
var h = content.substr(showChar, content.length - showChar);
var html = c + '<span class="more--ellipses">' + ellipsestext+ '&nbsp;</span><span class="more--content"><span>' + h + '</span> <a href="" class="more--link">' + moretext + '</a></span>';
$(this).html(html);
}
});
$(".more--link").on('click', function(){
if($(this).hasClass("less")) {
$(this).removeClass("less");
$(this).html(moretext);
} else {
$(this).addClass("less");
$(this).html(lesstext);
}
$(this).parent().prev().slideToggle();
$(this).prev().slideToggle();
return false;
});
});
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment