document.addEventListener('DOMContentLoaded', function () { document.querySelectorAll('.woocommerce-category-nav').forEach(function(nav) { const hamburger = nav.querySelector('.hamburger-menu'); const mainMenu = nav.querySelector('.main-menu'); const closeBtn = nav.querySelector('.close-menu'); if (!mainMenu) return; function closeAllSubmenus() { nav.querySelectorAll('li.open').forEach(li => li.classList.remove('open')); nav.querySelectorAll('.submenu-toggle').forEach(bt => bt.setAttribute('aria-expanded','false')); } if (hamburger) { hamburger.addEventListener('click', function (e) { e.stopPropagation(); mainMenu.classList.add('active'); }); } if (closeBtn) { closeBtn.addEventListener('click', function (e) { e.stopPropagation(); mainMenu.classList.remove('active'); closeAllSubmenus(); }); } document.addEventListener('click', function (e) { if (!nav.contains(e.target)) { mainMenu.classList.remove('active'); closeAllSubmenus(); } }); // Create per-parent submenu toggle buttons (idempotent) nav.querySelectorAll('li').forEach(function(li){ const submenu = li.querySelector('ul'); if (!submenu) return; if (li.querySelector('.submenu-toggle')) return; const a = li.querySelector('a'); let toggleButton = null; const existingArrow = a ? a.querySelector('.dropdown-arrow') : null; if (existingArrow) { toggleButton = document.createElement('button'); toggleButton.type = 'button'; toggleButton.className = 'submenu-toggle'; toggleButton.setAttribute('aria-expanded', 'false'); toggleButton.setAttribute('aria-label', 'Open submenu'); toggleButton.innerHTML = existingArrow.innerHTML || '▾'; existingArrow.parentNode.replaceChild(toggleButton, existingArrow); } else { toggleButton = document.createElement('button'); toggleButton.type = 'button'; toggleButton.className = 'submenu-toggle'; toggleButton.setAttribute('aria-expanded', 'false'); toggleButton.setAttribute('aria-label', 'Open submenu'); toggleButton.textContent = '▾'; if (a && a.nextSibling) a.parentNode.insertBefore(toggleButton, a.nextSibling); else if (a) a.parentNode.appendChild(toggleButton); else li.insertBefore(toggleButton, submenu); } toggleButton.addEventListener('click', function (ev) { ev.stopPropagation(); ev.preventDefault(); const isOpen = li.classList.toggle('open'); toggleButton.setAttribute('aria-expanded', isOpen ? 'true' : 'false'); Array.from(li.parentElement.children).forEach(function(child){ if (child !== li) { child.classList.remove('open'); const sibBtn = child.querySelector('.submenu-toggle'); if (sibBtn) sibBtn.setAttribute('aria-expanded','false'); } }); }); }); // DELEGATED: Force navigation for anchor clicks on mobile (except modifier keys & toggle clicks) mainMenu.addEventListener('click', function(e) { if (window.innerWidth > 768) return; // desktop: don't force // if clicked the toggle button itself, do nothing here if (e.target.closest('.submenu-toggle')) return; const a = e.target.closest('a'); if (!a) return; // Respect modifier keys: user wants new tab/window if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return; // If this anchor belongs to a parent li that has a submenu => force navigation const li = a.parentElement; if (li && li.querySelector('ul')) { // Force navigation immediately to the anchor's href. // This bypasses other scripts that call preventDefault(). const href = a.getAttribute('href'); if (href && href.trim() !== '#' && href.trim() !== '') { // Small safety: allow anchors with javascript: or void(0) to behave normally if (/^\s*(javascript:|void\(|#)\s*/i.test(href)) return; // Navigate window.location.href = href; } } }); // Cart button syncing (your second script) const cartBtn = document.getElementById('elementor-menu-cart__toggle_button'); if (cartBtn) { cartBtn.style.display = mainMenu.classList.contains('active') ? 'none' : ''; const mo = new MutationObserver(mutations => { mutations.forEach(m => { if (m.attributeName === 'class') { cartBtn.style.display = mainMenu.classList.contains('active') ? 'none' : ''; } }); }); mo.observe(mainMenu, { attributes: true }); } }); });

Husqvarna