I'm trying to convert some old jQuery code that manages the expanded/collapsed state of items in a navigation menu to pure JS and I've hit a roadblock.
HTML menu code:
<nav class="main-navigation">
<ul class="menu">
<li class="menu-item">
<a href="#">Item 1</a>
<button class="dropdown-toggle" aria-expanded="false">
<svg class="svg-icon"></svg>
<span class="screen-reader-text">Expand submenu</span>
</button>
<ul class="sub-menu">
<li class="menu-item">
<a href="/item-a/">Item a</a>
</li>
<li class="menu-item">
<a href="/item-b/">Item b</a>
</li>
<li class="menu-item">
<a href="/item-c/">Item c</a>
</li>
...
jQuery code:
var container = $( '.main-navigation' );
container.find( '.dropdown-toggle' ).on( 'click', function( e ) {
var _this = $( this );
e.preventDefault();
// Remove toggle classes and attributes from all menu items except the active one and its parents.
container.find( '.dropdown-toggle.toggle-on' )
.not( _this )
.not( _this.parents( '.children, .sub-menu' )
.prev( '.dropdown-toggle' )
)
.removeClass( 'toggle-on' )
.attr( 'aria-expanded', false );
...
} );
My attempts at pure JS code:
var container = document.querySelector( '.main-navigation' );
container.querySelectorAll( '.dropdown-toggle' ).forEach( item => {
item.addEventListener( 'click', function( e ) {
var _this = e.target;
e.preventDefault();
// Remove toggle classes and attributes from all menu items except the active one and its parents.
container.querySelector( '.dropdown-toggle.toggle-on' );
// How to convert the rest of the chained methods to pure JS?
???.classList.remove( 'toggle-on' ));
???.setAttribute( 'aria-expanded', false );
...
} );
} );
Basically, I don't know how to convert the not()
methods to pure JS. I've seen some people resort to the :not
CSS selector but I don't know if it's possible in this case.
Any help would be appreciated.