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 9147143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:55:00+00:00 2026-06-17T10:55:00+00:00

I am designing a website and have adapted a simple javascript script to display

  • 0

I am designing a website and have adapted a simple javascript script to display sub-links under every menu title.

The menu titles are stored in a list and each one houses individual hyperlinks and are revealed using a CSS file with the following classes:

.hidden {display: none;}
.unhidden {display: block;}

Based on this javascript function:

function unhide(liID) {
         var item = document.getElementById(liID);
         if (item) {
            item.className=(item.className=='hidden')?'unhidden':'hidden';
         }
        }

Here is a sample of the HTML file:

<a href="javascript:unhide('menu1');">First Menu</a>
<li id="menu1" class="hidden"> 
    <ul class="subMenus">
        <li>Sub menu 1 item 1</li>
        <li>Sub menu 1 item 2</li>
        <li>Sub menu 1 item 3</li>
        <li>Sub menu 1 item 4</li>
    </ul>
</li>
<a href="javascript:unhide('menu2');">Second Menu</a>
<li id="menu2" class="hidden"> 
    <ul class="subMenus">
        <li>Sub menu 2 item 1</li>
        <li>Sub menu 2 item 2</li>
        <li>Sub menu 2 item 3</li>
        <li>Sub menu 2 item 4</li>
    </ul>
</li>

The code works and performs well. But with the way it is coded, once a menu has been opened it must be clicked again for it to collapse. Otherwise the lists get really long and crowded after 3 or 4 menus are opened.

My question is…., what is the code required in the javascript to close the previously opened menu as it opens a new one to save space?

Thanks.

  • 1 1 Answer
  • 0 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-06-17T10:55:01+00:00Added an answer on June 17, 2026 at 10:55 am

    For keeping previous active menu in global variable you can hide previously selected menu, following js may helpful for you

    var activemnu="";
    function unhide(liID) {
    
      var item = document.getElementById(liID);
      if(liID!=activemnu && activemnu!="")
      {
         var activeitem= document.getElementById(activemnu);
         activeitem.className= 'hidden';
      }
    
      if (item) {
         item.className=(item.className=='hidden')?'unhidden':'hidden';
      }
      activemnu=liID;
    

    }

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

Sidebar

Related Questions

So I have been working with javascript for a website I am designing, shocker
I am designing a website and I want to have a static menu at
I have a simple website and I use masterPage for designing my template. everythings
I'm designing a simple website and I have a question. I want after all
Does anyone have some guidelines or links to articles when designing a website that
I'm designing a website right now, and have a menubar at the top for
I have this website I am designing and I want to make it to
I just learnt basic Servlets and JSP technology and am designing a simple website
For the last few days I have been designing a layout for my website
im designing a website and i have screen resolution problem. I want to scale

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.