/    Sign up×
Community /Pin to ProfileBookmark

Closing old submenu when clicking new one..

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 [B]<a href=”../public/tjanster.htm”>Tjänster</a>[/B] 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

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 11.2003 — try this:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;Basic menu&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
//&lt;![CDATA[&lt;!--
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
}
}
//--&gt;//]]&gt;
&lt;/script&gt;

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

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

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
Copy linkTweet thisAlerts:
@webkockenauthorNov 11.2003 — 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...
Copy linkTweet thisAlerts:
@FangNov 11.2003 — Change your DTD to:

[COLOR=blue]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

[/COLOR]


or read [URL=http://faq.ozoneasylum.com/834/]this[/URL]
Copy linkTweet thisAlerts:
@webkockenauthorNov 11.2003 — thanks for all your help....

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

Success!

Help @webkocken spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...