1
I am working on a project and came across the following situation: a list of items for a side navigation bar needs buttons that close and open to click (until then beauty). However I need to understand how I do so that when one is opened, the others that have already been opened will close.
<body>
<nav class="side-navbar">
<div>
<a href="#" class="main" id="titulo1">Titulo1</a>
<ul class="lista">
<li><a href="#">item1</a></li>
<li><a href="#">item2</a></li>
<li><a href="#">item3</a></li>
</ul>
</div>
<div>
<a href="#" class="main" id="titulo2">Titulo2</a>
<ul class="lista">
<li><a href="#">item1</a></li>
<li><a href="#">item2</a></li>
<li><a href="#">item3</a></li>
<li><a href="#">item4</a></li>
</ul>
</div>
<div>
<a href="#" class="main" id="titulo3">Titulo3</a>
<ul class="lista">
<li><a href="#">item1</a></li>
<li><a href="#">item2</a></li>
<li><a href="#">item3</a></li>
<li><a href="#">item4</a></li>
</ul>
</div>
</nav>
<script src="script.js"></script>
</body>
CSS
ul {
list-style: none;
display: none;
}
.main{
font-size: 22px;
display: block;
}
.toggle{
display: block;
}
.untuggle{
display: none;
}
JS
let main = document.querySelectorAll('.main');
let itens = document.querySelectorAll('.lista');
function toggleSide(togle, untugle){
for(let i = 0; i < main.length; i++){
main[i].addEventListener('click', function(){
});
}
}