Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How can I hide a drop down menu when clicking on other main nav?

    Hi.

    I'm having trouble making a main vertical navigation with two drop down menus and wondering if anyone can please help as I'm not great with javascript!
    I have a main menu and two drop down menus. At the moment I get them to open fine when the main link is clicked and close when it is clicked again. But I want dropdownmenu1 to close when opening dropdownmenu2 so that they are not open at the same time! Please help!

    This is my javascript:

    Code:
    <script type="text/javascript">
    function showElement(layer){
    var planLayer = document.getElementById(layer);
    
    if(planLayer.style.display=="none"){
    planLayer.style.display="block";
    
    planLayer.backgroundPosition="top";
    } else { 
    planLayer.style.display="none";
    }
    }
    </script>
    I put an onClick show Element in the html:

    Code:
    <li><a href="#" class="page2" onclick="showElement('plan-menu'); return false;">
    					<span>Unique Locations</span></a></li>

    Can I put something here to hide the other submenu?????

    this is my DHTML/CSS for the whole menu:

    Code:
    <ul class="Menu">
    
     <!--PLANNING main nav -->                
                        <li><a href="#" class="page2" onclick="showElement('plan-menu'); return false;">
    					<span>Unique Locations</span></a></li>
    				
    
    
    <!--PLANNING SUB MENU -->	                
    					<ul id="plan-menu" class="plan-menu" style="display:none;">
    					<li><a class="page2-1" href="p1.html"><span>Full Planning</span></a></li>
    					<li><a class="page2-2" href="p2.html"><span>Boutique Breaks</span></a></li>
    					<li><a class="page2-3" href="p3.html"><span>On the Day</span></a></li>
    					
    					</ul>
    <!--END PLANNING SUB MENU -->                    
                        
                        <li><a class="page3" href="uniqueLocations.html" ><span>Unique Locations</span></a></li>
                        
    	                <li><a href="#" class="page4" onclick="showElement('stat-menu'); hideElement('plan-menu'); return false;">
                        <span>Love &amp; Friendship</span></a></li>
                        
                        
    <!--	            WEDDING STATIONARY SUB MENU -->                    
                        <ul id="stat-menu" class="stat-menu" style="display:none;">
    					<li><a class="page2-1" href="p1.html"><span>Full Planning</span></a></li>
    					<li><a class="page2-2" href="p2.html"><span>Boutique Breaks</span></a></li>				
    					</ul>
                        
    <!--	            END WEDDING STATIONARY SUB MENU -->
    
    	                <li><a class="page5" href="servicesPrices.html"><span>Services &amp; Prices</span></a></li>
    	            </ul>

    Thanks!!!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    I'd recommend you to check http://htmldog.com/articles/suckerfish/dropdowns/, there is a tutorial on how to make a CSS based drop down, which is far more superiour that a javascript based one.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,467
    Thanks
    3
    Thanked 495 Times in 482 Posts
    Code:
    <script type="text/javascript">
    var Lst;
    
    function showElement(layer){
    var planLayer = document.getElementById(layer);
    if (Lst&&Lst!=planLayer) Lst.style.display="none";
    Lst=planLayer;
    if(planLayer.style.display=="none"){
    planLayer.style.display="block";
    
    planLayer.backgroundPosition="top";
    } else {
    planLayer.style.display="none";
    }
    }
    </script>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    iamerica (05-29-2009)

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks so much! That piece of code works brilliantly!
    Vic (or somebody else?) could you perhaps also show me how to adjust your code if there are three of four submenus? I will very soon get more content and I suspect have to introduce more submenus.

    Do I have to introduce some kind of loop then?

    I really appreciate your help! What a great site.

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ignore the last post. I just added two more submenus and the code still works fine so thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •