My Assignment: Hi! I am doing an assignment in school where I am supposed write code in Javascript in order to toggle visibility for the submenus each belonging to their own topmenu in a navigation bar for a webpage. The visibility should be set to hidden by default and should be shown when a topmenu is clicked on. I know how to toggle visibility for ONE submenu belonging to a topmenu, but fail to make my code work for multiple elements. My HTML-code:
        <a class="left_top1" onclick = "toggle()">Opinion</a><br>
            <div class="left_submenu_1" style="display: none;">
            <a class="left_sub1">Leaders</a><br>
            <a class="left_sub1">Debates</a><br>
            </div> 
<br>
            <a class="left_top2" onclick = "toggle()">Economy</a><br>
            <div class="left_submenu_2" style="display: none;">
            <a class="left_sub2">News</a><br>
            <a class="left_sub2">Your Economy</a><br>
            </div>
My Problem: The topmenus I speak of are "Opinion" and "Economy". The visibility of the div with the class "left_submenu_1" should be toggled when you click the topmenu "left_top1". Thus should the visibilily of the div with the class "left_submenu_2" be toggled when you click the topmenu "left_top2". This is what I fail to do. My Javascript code is so far:
function toggle() {
  var e = document.querySelectorAll("div.left_submenu_1, div.left_submenu_2");
  for (var i=0; i < e.length; i++) { // I know this will enable/disable the visibility for ALL elements selected from the querySelectorAll, which should NOT happen
    if(e[i].style.display == "none") {
      e[i].style.display = "block";
    } else if(e[i].style.display == "block") {
      e[i].style.display = "none";
    }
  }
}
Anyone who knows how to solve this issue of mine? I know there are errors in the for-loop (as I wrote next to it), but this is the best I can manage for now.
Please note: We are NOT allowed to use jQuery or to give the topmenus id:s, as the idea is to use one general function to toggle the visibility. Furthermore, the code which enables the toggle-function should be done in Javascript.
 
     
    