...

View Full Version : How can I hide a drop down menu when clicking on other main nav?



iamerica
05-29-2009, 12:44 PM
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:


<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:


<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:



<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!!!

abduraooft
05-29-2009, 01:10 PM
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.

vwphillips
05-29-2009, 01:50 PM
<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>

iamerica
05-29-2009, 09:29 PM
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.

iamerica
05-29-2009, 09:57 PM
Ignore the last post. I just added two more submenus and the code still works fine so thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum