I am making a menu with this HTML.
<div class="sidemenu-maincat">
<img src="folder/maincat1.jpg" alt="Main cat1" id="toggle" />
</div>
<div class="sidemenu-subcat">
<a href="submenu11.html"> - Submenu 11 </a>
</div>
<div class="sidemenu-subcat">
<a href="submenu12.html"> - Submenu 12 </a>
</div>
<div class="sidemenu-subcat">
<a href="submenu13.html"> - Submenu 13 </a>
</div>
<div class="sidemenu-maincat">
<img src="folder/main-cat2.jpg" alt="Main cat2 />
</div>
<div class="sidemenu-subcat">
<a href="submenu21.html"> > Submenu 21 </a>
</div>
<div class="sidemenu-subcat">
<a href="submenu22.html"> > Submenu 22 </a>
</div>
<div class="sidemenu-subcat">
<a href="submenu23.html"> > Submenu 23 </a>
</div>
I have the “sidemenu-subcat” div hidden and I would like it to show/hide when I press the pictures on in the “sidemenu-maincat” divs. I have tried to do this with Javascript but I can’t figure out how at all. I would like it not to be done with any library.
I’m keeping all my Javascripts in another document and this is the closest I’ve come to solve my problem:
function toggling()
{
if(document.getElementByClassName("hidden").style.display == "none")
{
document.getElementByClassName("hidden") = "block";
}
else
{
document.getElementByClassName("hidden") = "none";
}
}
function init()
{
var toggle = document.getElementsByClassName("toggle");
toggle.onclick = toggling;
}
I know it’s far from what that needs to be done but it’s the closest I’ve got..
Well I do prefer to use a library for this type of thing but I put together a pure javascript example for you. Though there is probably a better way to do it.
Firstly, each submenu should be contained within the it’s maincat div and given a hidden class so it is hidden on page load.
Working Example