-
Anonymous
Hello,
We have a left vertical main menu with one sub-menu (under the “Art tab”). Clicking on the “Art” tab correctly expands the sub-menu, however when any of the sub-pages are clicked on, the sub-menu collapses.
For both Desktop and Mobile, we would like the sub-menu to remain expanded when any of the sub-pages from that sub-menu are currently active. Similar to how the “Works” menu tab and expanded sub-menu works on the NK site (provided in the Private Info box).
The mobile menu currently uses the Off-Canvas Panel. The styling still needs work, but the basic functionality is there.
Is there a small code snippet we can add that would automatically apply the “toggled-on” class to <ul class=”submenu”> whenever one of the sub-pages is being viewed/active?
The site is currently hidden behind a Coming Soon page, so you will need to login to viiew. Credentials and the Site URL are provided in the Private Information box.
Thank you.
-
Alvind
Hi there,
That will require custom JavaScript. Here’s what I can came up with:
document.addEventListener('DOMContentLoaded', function() { // Check localStorage on page load and apply toggled state if needed const currentSubmenuId = localStorage.getItem('currentSubmenuId'); const currentPath = window.location.pathname; // Get all the links in the current submenu const currentSubmenu = document.getElementById(currentSubmenuId); if (currentSubmenu) { const submenuLinks = currentSubmenu.querySelectorAll('a'); const isCurrentPathInSubmenu = Array.from(submenuLinks).some(link => link.getAttribute('href').includes(currentPath) ); // Only keep submenu open if we're on one of its pages if (isCurrentPathInSubmenu) { currentSubmenu.classList.add('toggled-on'); } else { localStorage.removeItem('currentSubmenuId'); } } // Set up click handlers for all menu items const allMenuItems = document.querySelectorAll('.menu-item a'); allMenuItems.forEach(item => { item.addEventListener('click', function(e) { const clickedSubmenu = this.closest('.sub-menu'); // If this is a submenu item if (clickedSubmenu) { // Store this submenu's ID localStorage.setItem('currentSubmenuId', clickedSubmenu.id); // Ensure it stays open clickedSubmenu.classList.add('toggled-on'); // Remove toggled-on from all other submenus document.querySelectorAll('.sub-menu').forEach(menu => { if (menu.id !== clickedSubmenu.id) { menu.classList.remove('toggled-on'); } }); } else { // If clicking a main menu item, clear all submenus localStorage.removeItem('currentSubmenuId'); document.querySelectorAll('.sub-menu').forEach(menu => { menu.classList.remove('toggled-on'); }); } }); }); // Use MutationObserver to prevent unwanted closing const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.type === 'attributes' && mutation.attributeName === 'class') { const submenu = mutation.target; const currentSubmenuId = localStorage.getItem('currentSubmenuId'); if (currentSubmenuId === submenu.id) { const submenuLinks = submenu.querySelectorAll('a'); const isCurrentPathInSubmenu = Array.from(submenuLinks).some(link => link.getAttribute('href').includes(window.location.pathname) ); if (isCurrentPathInSubmenu && !submenu.classList.contains('toggled-on')) { submenu.classList.add('toggled-on'); } } } }); }); // Observe all submenus document.querySelectorAll('.sub-menu').forEach(submenu => { observer.observe(submenu, { attributes: true }); }); });
If this doesn’t work, you may need to reach out to a custom developer for assistance, as this is outside our support scope.
-
Anonymous
Thank you so much. This worked perfectly on Desktop!
For the mobile menu, since the hamburger menu closes and is regenerated every time a menu tab is clicked… is there some CSS or code that can easily keep the mobile sub-menus always expanded/open? So, once a visitor clicks the hamburger menu icon, they see the full, expanded mobile menu?
Would be better if visitors don’t have to click multiple times to view the sub-menu pages, plus since the mobile menu is hidden by default. I searched the web and the new support forum and tried a couple of the CSS-based solutions, but they didn’t seem to work properly.
-
Try this CSS:
#generate-slideout-menu.main-navigation ul ul { display: block !important; opacity: 1 !important; visibility: visible; height: auto; }
-
Anonymous
Fantastic. Works great. Appreciate the super fast reply!
-
Alvind
Glad to hear you got it working!
- You must be logged in to reply to this topic.