Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1050779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:49:18+00:00 2026-05-16T16:49:18+00:00

Good evening I’ve got a basic structure of ul, li and nested drop-down menus

  • 0

Good evening

I’ve got a basic structure of ul, li and nested drop-down menus in the form of ul’s. What I’m trying to do is that when the parent li is clicked, the child ul will show up. When the li is clicked again, the child ul hides.

So this is what I’ve got so far

$(document).ready(function() {
        $("ul#glass-buttons li").toggle(function() {
            $("ul", this).show();
        },
        function () {
            $("ul", this).hide();
        });
    });

It works perfectly, the only problem is that there are multiple child drop-down menus. So when one is open and I click another parent li, both child ul’s remain open and overlap. Can anyone recommend the best way I can hide all visible ul’s when they’re no longer required.

Additionally, what would be the best way to hiding any open ul when the user clicks anywhere in the document?

Thanks very much in advance. =]

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:49:19+00:00Added an answer on May 16, 2026 at 4:49 pm

    I couldn’t figure out how to do it with toggle. Your needs may be a bit too specialized for you to use toggle effectively. Here it is with click.

    <html>
      <head>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
          google.load("jquery", "1");
        </script>
        <script>
          function hide_sub_navs() {
            $('.top_level ul').hide().removeClass("shown");
          }
    
          $(function() {
            hide_sub_navs();
            $(".top_level").click(function(event) {
              event.stopPropagation();
              var clicked = this;
              var sub_menu = $(clicked).find("ul");
              if (sub_menu.hasClass("shown")) {
                sub_menu.hide().removeClass("shown");
              } else {
                sub_menu.show().addClass("shown");
                $(".top_level").each(function() {
                  var next_li = this;
                  if (next_li != clicked) {
                    $(next_li).find("ul").hide().removeClass("shown");
                  }
                });
              }
            });
            $(window).click(hide_sub_navs);
          });
        </script>
      </head>
      <body>
        <ul>
          <li class="top_level">top level 1
            <ul>
              <li>Sub level 1</li>
            </ul>
          </li>
          <li class="top_level">top level 2
            <ul>
              <li>Sub level 2</li>
            </ul>
          </li>
        </ul>
      </body>
    </html>
    

    Edit

    Changed

    $(‘body’).click(hide_sub_navs);

    to

    $(window).click(hide_sub_navs);

    If You don’t have any content in the page then the body tag gets short and you can’t click on it. If you ran the old solution on an actual web page it would probably work because you’d probably have other content propping the body tag up. Anyway, it looks like window works better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening all, I'm trying to write a method that creates and returns a
Good Evening, I have been trying to implement a simple Android(tm) application that utilizes
Good evening guys. I'm currently trying to get started on development of a project
Good evening, In my app that I'm currently developing, I have a class that
Good evening all, I've been working on an MD5 tool in C# that takes
Good evening everyone. I am puzzling over the oddity that the jQuery Marquee plugin
Good evening guys, I'm trying to pass multiple checkbox values through AJAX and process
Good evening, i need to know how can i have one .h file that
Good evening. I'm developing an android application that uses Facebook SDK. I've no problem
Good evening I have a JTable that I built with a TableModel how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.