Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
document.addEventListener("DOMContentLoaded", () => {
// Only run on touch-style devices (phones/tablets)
const isTouch = window.matchMedia("(hover: none), (pointer: coarse)").matches;
if (!isTouch) return;
// Optional: narrow scope to your header if you want.
// Example: const scope = document.querySelector("header");
const scope = document;
// Common WordPress/Elementor submenu patterns
const parentSelector = "li.menu-item-has-children > a, li.menu-item-has-children > .elementor-item";
// Close all open submenus inside scope
const closeAll = () => {
scope.querySelectorAll("li.menu-item-has-children.is-open").forEach((li) => {
li.classList.remove("is-open");
const a = li.querySelector(":scope > a, :scope > .elementor-item");
if (a) a.setAttribute("aria-expanded", "false");
});
};
// Click/tap outside closes
document.addEventListener("click", (e) => {
// If the click is outside the menu area, close
if (!e.target.closest(".elementor-nav-menu, .elementor-location-header, header")) {
closeAll();
}
});
// Make parent items toggle on first tap
scope.querySelectorAll(parentSelector).forEach((link) => {
link.addEventListener("click", (e) => {
const li = e.currentTarget.closest("li.menu-item-has-children");
if (!li) return;
const alreadyOpen = li.classList.contains("is-open");
// If submenu isn't open yet: open it and prevent navigation
if (!alreadyOpen) {
e.preventDefault();
e.stopPropagation();
// Close siblings/other open menus first (keeps things tidy)
closeAll();
li.classList.add("is-open");
link.setAttribute("aria-expanded", "true");
return;
}
// If it's already open, allow the click to navigate normally (2nd tap)
// (No preventDefault here)
});
});
});