I got the following snippet which shows or hide the the text as per the length of the text when clicked on Show More or Show Less button. Also, the Show More or Show Less button gets hidden if the characters are less than a particular limit say 200. This is working fine as you can see in the snippet. The problem the Show More button which should get hidden if characters are less than 200, is not working inside while loop. Rest all are working fine as needed. Only this particular section to hide the button is not working.
Full snippet:
var lengthofContent = $(".content").text();
console.log(lengthofContent.length);
if(lengthofContent.length < 200){
$(".show-more").hide();
}
$(".show-more span").click(function() {
var $this = $(this);
var $content = $this.parent().prev(".content");
var linkText = $this.text().toUpperCase();
if (linkText === "SHOW MORE") {
linkText = "Show less";
$content.toggleClass("hideContent showContent");
} else {
linkText = "Show more";
$content.toggleClass("showContent hideContent");
}
$this.text(linkText);
});
.text-container {
width: 500px;
margin: 0 auto;
}
.hideContent {
overflow: hidden;
line-height: 1em;
height: 55px;
}
.showContent {
line-height: 1em;
height: auto;
}
.showContent {
height: auto;
}
.show-more span {
cursor: pointer;
color: #27aae1;
}
.text-container {
padding: 10px;
background: #fff;
}
.text-container p {
line-height: 1.5;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="text-container">
<div class="content hideContent">
<p>
This is more than 200 characters. Lorem Ipsum is simply dummy text of the printing and typesetting. Lorem Ipsum is simply dummy text of the printing and typesetting.
</p>
</div>
<div class="show-more">
<span>Show more</span>
</div>
</div>
<div class="text-container">
<div class="content hideContent">
<p>
This is less than 200 characters. Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
<div class="show-more">
<span>Show more</span>
</div>
</div>
Code that doesn't work inside while loop.
var lengthofContent = $(".content").text();
console.log(lengthofContent.length);
if(lengthofContent.length < 200){
$(".show-more").hide();
}
The console.log displays the text count 680. That's the reason its not working. But not all the records from the database is above 200 characters. That is because it is adding the count of all the text together. But it should individually count for each record separately and work according to the count of characters of each post. How to do this?
While loop element.
<?php while($content = $contents->fetch()){ ?>
<div class="text-container">
<div class="content hideContent">
<p><?php echo $content['uc_desc']; ?></p>
</div>
<div class="show-more">
<span>Show more</span>
</div>
</div>
<?php } ?>
When you cut the text short to less than 200 characters and see what I am talking about. Show more button gets hidden and that's what not working inside while loop. Any help would be appreciated.