-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
26 lines (20 loc) · 1.1 KB
/
javascript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$( document ).ready(function() {
const listItem = document.getElementsByClassName("togglelist");
$('.toggle').on('click', function() {
$(this).prev('.list').find('.list-item--hide').toggle();
});
for (var i = 0; i < listItem.length; i++) {
const maxVisibleItems = $(listItem[i]).data('max-visible-items');
const totalItems = $(listItem[i]).find(".list-item").length;
const hiddenItems = totalItems - maxVisibleItems; // number of hidden items
const toggle = $(listItem[i]).find('.toggle'); // the toggle button
$(listItem[i]).find('.number').text(hiddenItems); // show number of hidden items
if (totalItems > maxVisibleItems) { // if total number of items is less than max visible items
const toggleItems = $(listItem[i]).find('.list-item:nth-last-child(-n+' + hiddenItems + ')'); // the hidden items
toggle.css('display', 'block'); // show toggle button
toggleItems.addClass('list-item--hide');
} else {
toggle.css('display', 'none'); // hide toggle button if totalItems is not larger than maxVisibleItems
}
}
});