Read More functionality not working properly when rendering html from database with the help of ejs

Viewed 277

I'm having this issue where I am rendering some HTML content from the database where I have to add a read more button, but in the case of a normal string the read more is working fine, but for HTML content rendering from DB it is running in reverse mode, for example: clicking on read more is doing read less work, and clicking on reading less it is doing read more work. I can't understand where the problem is, or am I missing something. Help will be appreciated.

Demonstration of the problem: https://playcode.io/632632/

Thanks in advance.

<div class="hero-content">
    <div>
        <span class="more">
            <%-showPost[0].postDescription%> 
        </span>
    </div>
</div>

<%-showPost[0].postDescription%> is rendering the below html code from database..

<h2>What is Lorem Ipsum?</h2>
<p><strong>Lorem Ipsum</strong>&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<h2>Why do we use it?</h2>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humor and the like).</p>
                                           

read more style and script are below:

<style>
    .morecontent span {
        display: none;
    }
    .morelink {
        display: block;
    }
</style>
<script>
    $(document).ready(function() {
    
    // Configure/customize these variables.
    var showChar=800;// How many characters are shown by default
    var ellipsestext = "...";
    var moretext = "Read More";
    var lesstext = "Read Less";


    $('.more').each(function() {
        var content = $(this).html();
        console.log('content=======',content)
        if(content.length > showChar) {
            console.log('====true===')
            var c = content.substr(0, showChar);
            var h = content.substr(showChar, content.length - showChar);
            console.log('====C===',c,'====H===',h)
            var html = c + '<span class="moreellipses">' + ellipsestext+ '&nbsp;</span><span class="morecontent"><span>' + h + '</span>&nbsp;&nbsp;<a href="" class="morelink">' + moretext + '</a></span>';

            $(this).html(html);
        }

    });

    $(".morelink").click(function(){
        if($(this).hasClass("less")) {
            $(this).removeClass("less");
            $(this).html(moretext);
        } else {
            $(this).addClass("less");
            $(this).html(lesstext);
        }
        $(this).parent().prev().slideToggle( "slow" );
        $(this).prev().slideToggle( "slow" );
        return false;
    });
});
</script>
0 Answers
Related