1
I’m trying to use querySelectorAll to see when <li>
is clicked and display the menu of each 1 the problem is having to keep specifying the array, example obj[0], for each 1, I wondered if I could do this more automatically? without having to specify for each <li>
different?
html:
<ul class="obj-menu">
<div class="obj-header">title</div>
<li class="obj-item">
<span class="obj-title">text1</span>
<ul class="obj-menu-sub">
<li class="obj-item-sub">texto 2</li>
<li class="obj-item-sub">texto 3</li>
<!-- <li class="obj-item-sub"></li> -->
</ul>
</li>
<li class="obj-item">
<span class="obj-title">title 2</span>
<ul class="obj-menu-sub">
<li class="obj-item-sub">texto 1</li>
<li class="obj-item-sub">texto 2</li>
<!-- <li class="obj-item-sub"></li> -->
</ul>
</li>
</ul>
What I managed to do so far in JS::
abreObj = document.querySelectorAll(".obj-item");
abreObj[1].addEventListener("click",function(event){
var objSub = abreObj[1].querySelector(".obj-menu-sub");
if(objSub.style.display == "block"){
objSub.style.display = 'none';
}else{
objSub.style.display = "block";
}
})
Obg, really helped, the explanation was very clear and gave to understand right!
– Lukas Takahashi