The default behavior of submenus on the tablet/mobile view of Elementor menus is to remain open when opening another top-level submenu. I have found that a better behavior would be to collapse any previously opened top-level submenus when opening a different top-level submenu. I wrote the function below to do that; let’s go through it.
Initially, I wrote this in VS Code and pasted into the console to test. Once I had it working, I wrapped it in a
The
The first if-statement jumps up a couple of elements and makes an id comparison. If the id matches the Elementor menu id, then the current index is a top-level item. If it fails this check, then it could be a nested submenu and gets skipped. Next we make sure the current index is not the submenu we just clicked (the one that called the function). If not, we can then check the current index to see if it is expanded. If it is expanded, we can go down to the first child element (the
The reason I went down to the span is because if we clicked the current index, the anchor tag, we would essentially be clicking that link and navigating to that page. It was through trial and error that I figured out the child element could be clicked to get the effect I wanted.
Last, we add a click listener to the menu items and run the function, passing the
window.addEventListener("load", () => {
const elementorMenu = jQuery("[id^='menu-2']");
const menuItemsWithSubmenu = jQuery("a.elementor-item.has-submenu");
function collapseOpenSubmenus(element) {
let clickedElement = jQuery(element);
menuItemsWithSubmenu.each(function (currentIndex) {
// If the menu item is top level
if (jQuery(menuItemsWithSubmenu[currentIndex]).parent().parent().id == elementorMenu.id) {
// If it is NOT the clicked (passed) element
if (jQuery(menuItemsWithSubmenu[currentIndex]) != clickedElement) {
// If it is expanded
if (jQuery(menuItemsWithSubmenu[currentIndex]).attr("aria-expanded") == "true") {
jQuery(menuItemsWithSubmenu[currentIndex]).children()[0].click();
}
}
}
});
}
menuItemsWithSubmenu.click(function () {
collapseOpenSubmenus(jQuery(this));
});
});