I am guessing the HTML
You need to delegate and no need for jQuery I use the :scope selector for the first time
I have updated the HTML and the JS to match your HTML.
I have also tried to tighten some of the selectors to match WP
Please note that if you already have a WP theme with JS that wants to toggle your menu, then my script will likely interfere and perhaps your question is one for SuperUser or the WP tag
document.addEventListener('DOMContentLoaded', function () {
const navSelectornav = document.querySelector('.main-nav';nav') || document;
const selector = 'li.menu-item-has-children > ul.sub-menu';
// Open /Hide closeall submenus on hoverinitially
documentnav.querySelectorAll(navSelectorselector).forEach(function +(ul) '{
ul.hidden = true;
});
// Hover open/close
nav.querySelectorAll('li.menu-item-has-children').forEach(function (li) {
const sub = li.querySelector(':scope > ul'ul.sub-menu');
if (!sub) return;
// Hide submenu initially
sub.hidden = true;
li.addEventListener('mouseenter', function () {
sub.hidden = false;
});
li.addEventListener('mouseleave', function () {
sub.hidden = true;
});
});
// Click to toggle submenus (touch devices + nested menus)
documentnav.addEventListener('click', function (e) {
const linka = e.target.closest(
navSelector + ' 'li.menu-item-has-children > a'
);
if (!linka) return;
const sub = linka.nextElementSibling;
if (!sub || sub.tagName !== 'UL' || !sub.classList.contains('sub-menu')) return;
// Toggle visibility
sub.hidden = !sub.hidden;
// PreventToggle linkonly, navigationdo whennot togglingnavigate on first tap
e.preventDefault();
// Prevent bubbling to any "click outside" handlers
e.stopPropagation();
});
// Optional: close all menus when clickingclick outside thecloses navigationall
document.addEventListener('click', function (e) {
const nav = document.querySelector(navSelector);
if (!nav || nav.contains(e.target)) return;
nav.querySelectorAll('.menu-item-has-children > ul'selector).forEach(function (ul) {
ul.hidden = true;
});
});
});
<nav class="main-nav" aria-label="Hoofdmenu">
<ul class="menu">
<li class="menu-item menu-item-has-children">
<a href="/how-we-support/">How we support</a>
<ul class="sub<!-menu">
- WordPress-rendered submenu -->
<li class="menu-item"><a<ul href="/how-we-support/optionclass="sub-1/">Option 1</a></li>
menu">
<li class="menu-item menu-item-hastype-children">post_type menu-item-object-page menu-item-2060">
<a href="/how-we-support/optionhref="#">Cross-2/">Optionfunctional 2<training & tools</a>
<ul class="sub-menu"></li>
<li class="menu-item"><aitem href="/howmenu-weitem-support/optiontype-2/subpost_type menu-1/">Subitem-object-page 1</a><menu-item-2061">
<a href="#">ESG Programme management support</li>a>
</li>
<li class="menu-item"><aitem href="/howmenu-weitem-support/optiontype-2/subpost_type menu-2/">Subitem-object-page 2</a></li>menu-item-2062">
<<a href="#">Industry collaboration & pre-competitive initiatives</ul>a>
</li>
<li class="menu-item"><aitem href="/howmenu-weitem-support/optiontype-3/">Optionpost_type 3<menu-item-object-page menu-item-2063">
<a href="#">Multi-stakeholder intiatives</a><a>
</li>
</ul>
</li>
<li class="menu-item"><a href="/about/">About</a></li>
<li class="menu-item"><a href="/contact/">Contact</a></li>
</ul>
</nav>