I encountered an issue while coding where the "read more" link works correctly, but the "show less" link does not. Despite my efforts, I cannot seem to identify the error.
Within this code snippet, there is an anchor tag with class="show-less"
that I am using to hide the span element with class="spanTxt"
. However, for some reason, it is not concealing the span element properly.
I'm puzzled by this situation - if I can use display:inline
in the read more link, why isn't the display:none
working as intended in the show less link?
Any assistance would be greatly appreciated :)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Add Read More Link</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var maxLength = 300;
$(".show-read-more").each(function(){
var myStr = $(this).text();
if($.trim(myStr).length > maxLength){
var newStr = myStr.substring(0, maxLength);
var removedStr = myStr.substring(maxLength, $.trim(myStr).length);
$(this).replaceWith('<p class="show-read-more">'+newStr+'<a class="read-more" href="#">read more...</a>'+'<span id="hoja" class="spanTxt" style="display:none">'+removedStr+'</span>'+'</p>');
}
});
$(".read-more").click(function(){
$(".spanTxt").css("display","inline");
$(".show-read-more").append('<a href="#" class="show-less">show less...</a>');
$(this).hide();
});
$(".show-less").click(function(){
$(".spanTxt").css("display","none");
});
});
</script>
<style type="text/css">
</style>
</head>
<body>
<p class="show-read-more">Ut auctor velit sed consectetur rhoncus. Nunc
dictum facilisis felis nec facilisis. Integer nec justo vitae orci cursus
fermentum. Fusce semper, mi non tempus congue, velit leo efficitur quam,
laoreet venenatis libero felis et lacus. Pellentesque mattis hendrerit
nisi gravida hendrerit. Mauris sagittis tincidunt scelerisque. Vivamus lectus
erat, dictum et magna quis, iaculis finibus nisl. Aliquam quis ante odio. Eti
am tincidunt tellus tristique turpis tincidunt, eget condimentum urna rutrum.
Donec maximus consequat dolor, sit amet condimentum ipsum gravida ac. Etiam p
osuere tellus mauris, et dignissim nisl rutrum quis. Mauris tincidunt ante
sed velit maximus, vel tincidunt leo imperdiet. Morbi nec lac
us et metus semper porttitor. Sed pellentesque ex at
pellentesque scelerisque. Aliquam placerat gravida tortor, in fermentum ante
commodo quis. Etiam vehicula elementum quam. Aliquam eu augue eu lacus digni
ssim efficitur. Proin ex metus, ornare placerat nisi at, porta lobortis turpi
s. Praesent euismod nec nulla ultrices maximus. Vivamus imperdiet quam ac
lobortis cursus. Nam dapibus ullamcorper magna vehicula aliquam. Vivamus
hendrerit molestie neque. Ut interdum diam a purus ultrices facilisis.
Suspendisse molestie tempor dolor, sed tristique enim sagittis vitae. I
nteger eu dignissim lectus, commodo eff
icitur metus. Morbi quis justo finibus, interdum sem quis, imperdiet tellus
andit vel magna nec elementum. Vivamus tempor, urna pharetra euismod euis
mod, elit elit tincidunt sem, ut consectetur arcu massa non diam.
Etiam scelerisque nisi magna. Nulla facilisi.
Sed pharetra nunc lectus, in maximus dolor ornare sit amet.</p>
</body>
</html>