Click to See Complete Forum and Search --> : Closing old submenu when clicking new one..


webkocken
11-10-2003, 06:32 PM
Hi!

I have this menu that works really good, but i want it to do one thing:
When a submenu is open, and I click a new link (not in the same submenu) i want the open submenu to close again.

I'm not at all good at js so i reallt would appreciate som help.

The code looks like this:

<SCRIPT>
function menu(obj) {
if(obj.style.display=='none')
{
obj.style.display=''
}
else obj.style.display='none'
}
</SCRIPT>

and body looks like this:

<a href="../public/start.htm">Hem</a><br>
<a href="../public/foretaget.htm">Företaget</a><br>
<SPAN OnClick="menu(tjanster)">
<a href="../public/tjanster.htm">Tjänster</a></SPAN><br>
<SPAN STYLE="display: none" ID="tjanster">
<a href="../public/webdesign.htm">Webdesign</a><br>
<a href="../public/positionering.htm">Positionering</a><br>
<a href="../public/webshop.htm">Webshop</a><br>
</SPAN>
<SPAN OnClick="menu(simple)">
<a href="../public/simpleupdate.htm">SimpleUpdate</a>
</SPAN><br>
<SPAN STYLE="display: none" ID="simple">
<a href="../public/demo.htm">Demo</a><br>
<a href="../public/kop.htm">Köp</a><br>
</SPAN>
<SPAN OnClick="menu(referenser)">
<a href="../public/referenser.htm">Referenser</a>
</SPAN><br>
<SPAN STYLE="display: none" ID="referenser">
<a href="../public/referenser_web.htm" target="main">Webdesign</a><br>
<a href="../public/referenser_partners.htm">Partners</a>
<br></SPAN>
<a href="../public/kontakt.htm">Kontakt</a><br>
<a href="../public/support.htm">Support</a><br>
<a href="../public/jobb.htm" target="main">Jobb</a>

So, if i click the link <a href="../public/tjanster.htm">Tjänster</a> the submenu opens, and i want it to close if i click another link in the menu, but not in the submenu that is open.

Please help me....

Thanks in advance
/Lari

Fang
11-11-2003, 07:53 AM
try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Basic menu</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">
//<![CDATA[<!--
var OpenMenu=""; //id of menu open
function menu(objID) {
obj=document.getElementById(objID);
if(obj.style.display=='block') { //item is open close it
obj.style.display='none';
OpenMenu=""; // no menu open
}
else { //item is closed open it
if(OpenMenu!="") { // if a menu is open close it
document.getElementById(OpenMenu).style.display='none';
}
obj.style.display='block';
OpenMenu=objID; //save menu open
}
}
//-->//]]>
</script>

<style type="text/css">
<!--
#menu {width:6em; background:#aaa;}
#menu div {display:none; border:1px solid red;}
#menu a {display:block;}
-->
</style>

</head>
<body>
<div id="menu">
<a href="../public/start.htm">Hem</a>
<a href="../public/foretaget.htm">F&ouml;retaget</a>
<a href="../public/tjanster.htm" onclick="menu('tjanster'); return false;">Tj&auml;nster</a>
<div id="tjanster">
<a href="../public/webdesign.htm">Webdesign</a>
<a href="../public/positionering.htm">Positionering</a>
<a href="../public/webshop.htm">Webshop</a>
</div>
<a href="../public/simpleupdate.htm" onclick="menu('simple'); return false;">SimpleUpdate</a>
<div id="simple">
<a href="../public/demo.htm">Demo</a>
<a href="../public/kop.htm">K&ouml;p</a>
</div>
<a href="../public/referenser.htm" onclick="menu('referenser'); return false;">Referenser</a>
<div id="referenser">
<a href="../public/referenser_web.htm" target="main">Webdesign</a>
<a href="../public/referenser_partners.htm">Partners</a>
</div>
<a href="../public/kontakt.htm">Kontakt</a>
<a href="../public/support.htm">Support</a>
<a href="../public/jobb.htm" target="main">Jobb</a>
</div>
</body>
</html>

The menu link is only opened if JavaScript is not used.
<a> in <span> not valid
special characters to entities
target is not valid here, but depends on the DTD used
I won't answer the same thread on the other forum

webkocken
11-11-2003, 08:28 AM
Thanks alot dude!

One problem only...when opening the submenu by clicking the link, i really need to be able to use target....is that possible somehow?

Thx again...

Fang
11-11-2003, 01:23 PM
Change your DTD to:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

or read this (http://faq.ozoneasylum.com/834/)

webkocken
11-11-2003, 01:47 PM
thanks for all your help....

i solved the problem by removing return false in the link...and the menu works perfectly...so thx again!!