I am seeking a solution to open only one node at a time on click, and remove the 'is-active' class from other items.
Currently, there is excessive space taken up after opening multiple menu items. I want to ensure that only one node is open at a time by removing the 'is-active' class from the previous sub-item when switching to another sub-item.
The red square is used to open the item - could you assist me with this?
const nav = document.querySelector('.menu-primary');
// const navElement = nav.querySelectorAll('.menu-item-has-children');
const navElementIcon = nav.querySelectorAll('.js-navigation-arrow');
// console.log(navElesment);
navElementIcon.forEach(function (element) {
element.addEventListener("click", function() {
let elementParent = element.closest('.menu-item-has-children');
if (elementParent.classList.contains('is-active')) {
elementParent.classList.remove('is-active');
} else {
elementParent.classList.add('is-active');
}
});
});
.menu-primary a {
margin-right: 30px !important;
}
.menu-primary > li {
position: relative;
text-align: left;
}
.menu-primary > li:first-child .menu-item a {
margin-top: 0;
}
.menu-primary > li:nth-child(4) {
margin-top: 0;
}
.menu-primary > li > .menu-item-icon {
top: 7px;
}
.menu-primary > li .sub-menu {
position: static;
transform: none;
display: none;
opacity: 1;
visibility: visible;
}
.menu-primary > li .sub-menu > .sub-menu__list > li > .sub-menu__child-list {
padding-left: 15px;
}
.menu-primary > li > a {
display: inline-block;
font-size: 16px;
margin: 8px 0;
}
.menu-primary a {
display: inline-block;
font-size: 14px;
}
.menu-item-icon {
display: flex;
width: 15px;
height: 15px;
justify-content: center;
align-items: center;
position: absolute;
top: 7px;
right: 0;
font-size: 8px;
color: #fff;
transform: rotate(180deg);
transition: 0.2s;
background: red;
z-index: 10;
}
.menu-item.is-active > .sub-menu {
display: block;
}
.menu-item {
position: relative;
margin: 5px 0;
}
.sub-menu__child-list {
display: none;
padding-left: 15px;
}
.menu-item.is-active > .sub-menu__child-list {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="menu-primary">
// Menu items HTML code here
</ul>