Update to include basic Fiddle: http://jsfiddle.net/g28nc89t/1/
Say I have a set of divs using a repeating pattern, not an unordered list, that follows this structure:
<div class="one cards">
<div class="random_cms-class"><div class="card">...</div></div>
<div class="random_cms-class"><div class="card">...</div></div>
<div class="random_cms-class"><div class="card">...</div></div>
</div>
<div class="two cards">
<div class="random_cms-class"><div class="card">...</div></div>
<div class="random_cms-class"><div class="card">...</div></div>
<div class="random_cms-class"><div class="card">...</div></div>
</div>
Using jQuery, how can I hide every 'card' after the third item and create a toggle to show/hide the hidden cards. Considering the card UI utlizes the same class, I do not want the toggle to show/hide all card blocks only the cards for that section. I can add id's or more specific classes to each card block.
I'm working with the following js, which is currently toggling every card instance vs only toggling for that section. It also flip flops the Show More and Show Less between the two blocks:
$('.cards').each(function(){
var max = 2
if ($(this).find(".card").length > max) {
$(this)
.find('.card:gt('+max+')')
.hide()
.end()
.append(
$('<div class="toggle"><a class="more">Show More</a></div>').click( function(){
$('.card:gt('+max+')').toggle();
if ( $('.more').length ) {
$(this).html('<a class="less">Show Less</a>');
} else {
$(this).html('<a class="more">Show More</a>');
};
})
);
}
});
The site is responsive and while I'd like to have some kind of slideup / slidedown animation I don't think it's practical in this scenario. Any help would greatly be appreciated. I tried using this stack as a guide: Jquery, hide & show list items after nth item