Forums

Give help. Get help.

  • # January 3, 2018 at 5:03 pm

    Hi. How are you ? I am trying to perform in simple menu in html and css

    I want the sub elements to be hidden, and when the user clicks the parent element, the sub elements are displayed dropdown.

    When the user clicks on category a or b, the sub elements are displayed

    # January 3, 2018 at 7:39 pm

    This would be one way, in vanilla JS:

    var link = document.getElementsByTagName('a');
    
    for (var i = 0; i < link.length; i++) {
    
      var target = link[i].nextElementSibling;
    
      if (!target) continue;
    
      target.style.display = 'none';
    
      link[i].addEventListener('click', function(e) {
    
        e.preventDefault();
    
        var aim = this.nextElementSibling;
    
        if (aim.style.display == 'none') aim.style.display = 'block';
        else aim.style.display = 'none';
      });
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag