How to make it so that when you click on the button again, it hides its menu
The script works so that when opening another “btn”, the one that is active is hidden, + if you click outside the menu field, all are hidden.
How do I make it so that when I click on the active button again, its menu is hidden
<ul class="first_menu">
<li class="menu_item">
<button class="menu_btn">1 btn</button>
<div class="dropdown">
<ul class="dropdown__list">
<li class="dropdown__item">
<a href="#" class="dropdown__link">1 Подпункт</a>
</li>
<li class="dropdown__item">
<a href="#" class="dropdown__link">2 Подпункт</a>
</li>
<li class="dropdown__item">
<a href="#" class="dropdown__link">3 Подпункт</a>
</li>
</ul>
</div>
</li>
<li class="menu_item">
<button class="menu_btn">2 btn</button>
<div class="dropdown">
<ul class="dropdown__list">
<li class="dropdown__item">
<a href="#" class="dropdown__link">1 Подпункт</a>
</li>
<li class="dropdown__item">
<a href="#" class="dropdown__link">2 Подпункт</a>
</li>
<li class="dropdown__item">
<a href="#" class="dropdown__link">3 Подпункт</a>
</li>
</ul>
</div>
</li>
<li class="menu_item">
<button class="menu_btn">3 btn</button>
<div class="dropdown">
<ul class="dropdown__list">
<li class="dropdown__item">
<a href="#" class="dropdown__link">1 Подпункт</a>
</li>
<li class="dropdown__item">
<a href="#" class="dropdown__link">2 Подпункт</a>
</li>
<li class="dropdown__item">
<a href="#" class="dropdown__link">3 Подпункт</a>
</li>
</ul>
</div>
</li>
</ul>
.first_menu {
position: relative;
justify-content: center;
display: flex;
}
.menu_item,.dropdown__item {
list-style-type: none;
}
.menu_btn {
cursor: pointer;
border: none;
padding: 25px;
background-color: black;
color: #ffffff;
font-weight: bold;
}
.dropdown {
transition: all 1s;
position: absolute;
top: 0;
transform: translateY(-100%);
background-color: black;
}
.dropdown__item {
text-align: center;
padding-bottom: 20px;
}
.dropdown__link {
text-decoration: none;
color: white;
font-weight: bold;
}
.active {
top: 100px;
transition: all 1s;
transform: translateY(-30%);
}
const btn = document.querySelectorAll('.menu_btn')
btn.forEach(function(item, i){
item.addEventListener('click', (e) => {
let currentBtn = e.currentTarget;
let nextElemt = currentBtn.nextElementSibling;
document.querySelectorAll('.dropdown.active').forEach(function(item, i) {
item.classList.remove('active')
})
nextElemt.classList.add('active')
const menuOff = document.querySelector('.first_menu');
document.addEventListener('click', function(e) {
const click = e.composedPath().includes(menuOff);
if(!click) {
nextElemt.classList.remove('active')
}
})
})
})