Hello guys, I am working on a project and stuck at a point. Please help me out.
There are multiples UL and each UL have some id, they all are static and for each UL there will be a See more button, by default the See More button will be hidden and if List length exceed with 5 the button will show.
With help of Jquery I have get the count of each UL li, but how to show the see more button.
.filter-list ul li:nth-child(n + 6) {
display: none;
}
//By default more button will be hidden and have to show if LI get exceed with 5
.filter-list label.more {
color: #f86843;
font-weight: 600;
font-style: oblique;
display: none;
}
Well you are 99% of the way there. Just select the see more button in relation to each filter-list.
$('.filter-list ul').each(function () {
liCount = $(this).children('li').length;
// If LI count is greater than or equal to 5, find the '.more' button of filter-list and show it.
if (liCount >= 5) {
$(this).parent().find('.more').show();
}
console.log(liCount);
});
So as you can see we just use the count to then trigger an if statement where we find the .more class button and show it.
I was just about to post this but was beaten by @Martyr2 above
I’ll post anyway as it may be of some use as I added a click event for the more button to show the extra items.
Obviously you would either then need to remove the more button as there is no more to see or make it say “see less” and do the reverse of what just happened.
It now seems clear that we can use toggleClass instead of add and remove, and because the lines are now the same we can pull it up out of the if statement.
Here is also a version that uses no jQuery. The code when it’s not running on codePen has the more sections appearing instantly now, instead of after a delay.
const lists = document.querySelectorAll(".filter-list");
lists.forEach(function (list) {
const liCount = list.querySelectorAll("li").length;
if (liCount > 5) {
list.querySelector(".more").classList.add("showMe");
}
});
const moreEls = document.querySelectorAll(".more");
moreEls.forEach(function (more) {
more.addEventListener("click", function (evt) {
const more = evt.target;
const ul = more.previousElementSibling;
ul.querySelector("li").forEach(function (li) {
li.toggleClass("showList");
});
more.innerHTML = more.innerHTML.includes("more")
? "See less..."
: "See more...";
});
});