Last active
March 17, 2022 17:17
-
-
Save jenhuls/5586d7ac2a017c4edb16a389749b5b5a to your computer and use it in GitHub Desktop.
[jQuery Read More] #jquery
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
.description .more--content span { | |
display: none; | |
} | |
.description .more--link { | |
display: 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
(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+ ' </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