I have a section where I’m pulling blog posts and showcasing them all. By default, 13 .resourceCard
‘s are show and then when clicking load more, 9 more should show.
However, when clicking the load more button, it only showcases the next 9, but not anything after that on subsequent click (doens’t show cards 23+).
Using hiddenCard.slice(x, 9)
doesn’t work either as only the console log appears.
Where is my logic falling apart?
Demo
$(function () {
var loadmoreBtn = $('.resourceListing__loadmore');
var hiddenCard = $('.insertCard:hidden');
var x = 13;
loadmoreBtn.on('click', function (e) {
e.preventDefault();
x = x + 9;
console.log("click");
hiddenCard.slice(0, 9).fadeIn().addClass("insertCard--flex");
if(hiddenCard.length == 0){
loadmoreBtn.hide();
}
});
});
:root {
--black: #000000;
--white: #FFFFFF;
--navy: #0E185F;
}
.placeholderCard,
.resourceCard {
padding: 60px;
border: 1px solid var(--black);
margin-bottom: 30px;
width: 100%;
}
.placeholderCard {
background: var(--navy);
color: var(--white);
padding: 20px;
}
.resourceListing {
padding: 80px 0;
}
.resourceListing__loadmore {
display: flex;
justify-content: center;
align-items: center;
margin: 60px 0;
cursor: pointer;
}
.resourceListing .insertCard:nth-child(n+16) {
display: none;
}
.insertCard {
display: flex;
}
.insertCard--flex {
display: flex !important;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<main class="resourceListing">
<div class="container">
<div class="row resourceListing__posts">
<div class="col-4 insertCard">
<div class="resourceCard">Card 1</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 2</div>
</div>
<div class="col-4 insertCard">
<div class="placeholderCard">This card is not part of the slice</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 3</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 4</div>
</div>
<div class="col-4 insertCard">
<div class="placeholderCard">This card is not part of the slice</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 5</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 6</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 7</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 8</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 9</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 10</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 11</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 12</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 13</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 14</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 15</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 16</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 17</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 18</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 19</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 20</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 21</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 22</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 23</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 24</div>
</div>
<div class="col-4 insertCard">
<div class="resourceCard">Card 25</div>
</div>
</div>
<div class="row">
<div class="col-12">
<a class="resourceListing__loadmore">Load more</a>
</div>
</div>
</div>
</main>
4
Answers
The issue here is that you get list of hidden cards once. Then process 9 items from that list without updating the list. So next time it processes the same 9 cards again.
To fix that you need update the list with:
Alternatively, you could move
var hiddenCard = $('.insertCard:hidden');
intoclick
functionP.S.
You should use
const
andlet
instead ofvar
After making a codepen of your code, I noticed you are not updating your ‘hiddenCard’ array after loading more. If you simply update the array, you code will work.
You can start by removing this line:
because no need a counter! And then move this line:
to beginning of
click
event after:Now with this changes, every time you click on Load More button, you create a new collection of remaining hidden cards and then make a slice of first 9 hidden cards.
Accourding to the jQuery documents:
Update
After all these changes, to hide the Load Mode button, you need to update your
if
statement to something like this:Good luck!