(function () { const btn = document.getElementById('chapterMenuBtn'); const dropdown = document.getElementById('chapterDropdown'); if (btn && dropdown) { function closeDropdown () { dropdown.classList.remove('open'); btn.setAttribute('aria-expanded', 'false'); } btn.addEventListener('click', () => { const isOpen = dropdown.classList.toggle('open'); btn.setAttribute('aria-expanded', isOpen ? 'true' : 'false'); }); // Click outside closes it document.addEventListener('click', (e) => { if (!e.target.closest('.chapterPicker')) closeDropdown(); }); // Smooth scroll when selecting a chapter dropdown.addEventListener('click', (e) => { const target = e.target.closest('button[data-target]'); if (!target) return; const id = target.getAttribute('data-target'); const el = document.getElementById(id); if (el) { el.scrollIntoView({ behavior: 'smooth', block: 'start' }); closeDropdown(); history.replaceState(null, '', '#' + id); // optional: update URL fragment } }); } const scrollLinks = document.getElementsByClassName('scrollLink'); for (let i = 0; i < scrollLinks.length; i++) { scrollLinks[i].addEventListener('click', (e) => { const target = e.target.closest('a[data-target]'); console.log("Scrolling to " + target); if (!target) return; const id = target.getAttribute('data-target'); const el = document.getElementById(id); if (el) { el.scrollIntoView({ behavior: 'smooth', block: 'start' }); closeDropdown(); history.replaceState(null, '', '#' + id); // optional: update URL fragment } }); } })();