-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbook.js
More file actions
54 lines (48 loc) · 1.74 KB
/
book.js
File metadata and controls
54 lines (48 loc) · 1.74 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
document.addEventListener('DOMContentLoaded', function() {
var checkboxes = document.querySelectorAll('input[name="book"]');
var bookItems = document.querySelectorAll('.book-list li');
var allCheckbox = document.getElementById('all');
function filterBooks() {
var selectedCategories = [];
checkboxes.forEach(function(checkbox) {
if (checkbox.checked && checkbox.value !== 'all') {
selectedCategories.push(checkbox.value);
}
});
if (selectedCategories.length === 0) {
bookItems.forEach(function(item) {
item.style.display = 'block';
});
} else {
bookItems.forEach(function(item) {
var itemCategories = item.getAttribute('data-category') || '';
var categoryList = itemCategories.split(' ');
var shouldShow = selectedCategories.some(function(cat) {
return categoryList.indexOf(cat) !== -1;
});
item.style.display = shouldShow ? 'block' : 'none';
});
}
}
allCheckbox.addEventListener('change', function() {
if (this.checked) {
checkboxes.forEach(function(checkbox) {
if (checkbox.value !== 'all') {
checkbox.checked = false;
}
});
}
filterBooks();
});
checkboxes.forEach(function(checkbox) {
if (checkbox.value !== 'all') {
checkbox.addEventListener('change', function() {
if (this.checked) {
allCheckbox.checked = false;
}
filterBooks();
});
}
});
filterBooks();
});