How to "Read more – Read less only if text is overflowing (else hide read more)"

  css, html, javascript, jquery

I am quite nascent with JavaScript and stuck with a bit complex problem.

I want to call "Read more" if body/container has "overflow-text" or "height > given height of "body".
else { hide "Read more" }

not able to do it on my own.
I am pasting my sample of simple read more / read less here.
Request you to please help me improvise it.

const readMoreBtn = document.querySelector('.read-more-btn');
const text = document.querySelector('.text');

readMoreBtn.addEventListener('click',(e)=>{
    text.classList.toggle('show-more');
    if(readMoreBtn.innerText === 'Read more'){
    readMoreBtn.innerText = 'Read less';
    }
    else{
    readMoreBtn.innerText = 'Read more';
    } 
});
<style>
body{
font-family: Arial;
text-align: justify;
max-width: 600px;
padding: 20px;
background-color: #f0f0f0;
}
.moreText{
display:none;
}
.read-more-btn{
padding: 15px 60px;
border: 1 px solid #828282;
outline: none;
cursor:pointer;
}
.text.show-more .moreText{
display:inline;
}
</style>
<body>
<h1>Hello World</h1>
<p class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa.<span class="moreText"> Fusce luctus vestibulum augue ut aliquet. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta.</span></p>
<button class="read-more-btn">Read more</button>
</body>

Source: Ask Javascript Questions

LEAVE A COMMENT