@include('front.landing-pages.medications.filter-categories', ['categories' => $categories])
document.querySelectorAll('.category-checkbox').forEach(checkbox => {
checkbox.addEventListener('change', function() {
const value = this.value;
// Handle all descendants
function checkDescendants(parentValue, checked) {
const children = document.querySelectorAll(`[data-parent="${parentValue}"]`);
children.forEach(child => {
child.checked = checked;
checkDescendants(child.value, checked);
});
}
// Handle all ancestors
function checkAncestors(childBox) {
const parentId = childBox.dataset.parent;
if (!parentId) return;
const parent = document.querySelector(`input[value="${parentId}"]`);
if (parent) {
const siblings = document.querySelectorAll(`[data-parent="${parentId}"]`);
parent.checked = Array.from(siblings).some(sibling => sibling.checked);
checkAncestors(parent);
}
}
// Apply changes
checkDescendants(value, this.checked);
checkAncestors(this);
});
});