Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Menu for display sub items in a list

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #264865
    Mafer
    Participant

    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
    https://codepen.io/aguiarima/pen/MrvQPM

    #264879
    Shikkediel
    Participant

    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)
  • The forum ‘JavaScript’ is closed to new topics and replies.