PDA

View Full Version : Switch Menu script



seisuki
05-18-2005, 05:58 AM
SCRIPT: Switch Menu
http://www.dynamicdrive.com/dynamicindex1/switchmenu.htm

Please help this novice. Thank you.
I'm trying to create a nested menu using the switch script but I cant seem to get it to work.

I'm trying to get to this:
FISH Category
- Fresh water fish
-- Molly
-- Guppy
-Salt water fish
- Blowfish
REPTILE Category
- Gecko
- Snake
-- Cobra

but with the changes I made, it wont display the other submenu (Salt Water Fish, Snake)...this is all i get
FISH Category
- Fresh water fish
-- Molly
-- Guppy
REPTILE Category
- Gecko

I think the problem lies in having 2 SPAN with id="sub1" in the <BODY> tag.
view it here: http://www.geocities.com/soul_astray/switchmenu.html
Thanks for the help.


<html>
<head>
<style type="text/css">
.menutitle{
cursor:pointer;
margin-bottom: 5px;
background-color:#9bc83a;
color:#000000;
width:440px;
padding:2px;
text-align:center;
font-weight:bold;
font-family:verdana;
font-size: 10px;
/*/*/border:1px solid #000000;/* */
}
.subtitle{
cursor:pointer;
margin-bottom: 5px;
background-color: #daf69d;
color:#000000;
width:440px;
padding:2px;
text-align:left;
font-weight:bold;
font-family:verdana;
font-size: 10px;
border:1px dotted #000000;
}

.submenu{
margin-bottom: 0.5em;
font-family:verdana;
font-size: 10px;
}
</style>

<script type="text/javascript">

/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.subtitle{display: none;}\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}

function SwitchMenu(obj){
if(document.getElementById){
var el = document.getElementById(obj);
var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
if(el.style.display != "block"){ //DynamicDrive.com change
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu" || ar[i].className=="subtitle" ) //DynamicDrive.com change
ar[i].style.display = "none";
}
el.style.display = "block";
}else{
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu" || ar[i].className=="subtitle" ) //DynamicDrive.com change
ar[i].style.display = "none";
}
el.style.display = "none";
}

}
}function SwitchMenu2(obj){
if(document.getElementById){
var el = document.getElementById(obj);
var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
if(el.style.display != "block"){ //DynamicDrive.com change
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu") //DynamicDrive.com change
ar[i].style.display = "none";
}
el.style.display = "block";
}else{
el.style.display = "none";
}

}
}
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function onloadfunction(){
if (persistmenu=="yes"){
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=get_cookie(cookiename)
if (cookievalue!="")
document.getElementById(cookievalue).style.display="block"
}
}

function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener)
window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate

</script>
</head>
<body>

<!-- Keep all menus within masterdiv-->
<div id="masterdiv">

<div class="menutitle" onclick="SwitchMenu('sub1')">FISH</div>
<span class="subtitle" onclick="SwitchMenu2('sub001')" id="sub1">Fresh Water</span>
<span class="submenu" id="sub001">
- Molly<br>
- Guppy<br>

</span>
<span class="subtitle" onclick="SwitchMenu2('sub002')" id="sub1">Salt Water</span>
<span class="submenu" id="sub002">
- Blowfish
</span>
<div class="menutitle" onclick="SwitchMenu('sub2')">Reptiles</div>
<span id="sub2" class="subtitle" onclick="SwitchMenu2('subsub2')">Gecko</span>
<span class="submenu" id="subsub2">
- <a href="notice.htm">Usage Terms</a><br>
- <a href="faqs.htm">DHTML FAQs</a><br>
- <a href="help.htm">Scripts FAQs</a>
</span>

<div class="menutitle" onclick="SwitchMenu('sub3')">Dogs</div>
<span id="sub3" class="subtitle" onclick="SwitchMenu2('subsub3')">Dogs</span>
<span class="submenu" id="subsub3">
- <a href="http://www.codingforums.com">Coding Forums</a><br>
</span>

<div class="menutitle" onclick="SwitchMenu('sub4')">Cats</div>
<span id="sub4" class="subtitle" onclick="SwitchMenu2('subsub4')">Cats</span>
<span class="submenu" id="subsub4">
- <a href="http://www.javascriptkit.com">JavaScript Kit</a><br>
- <a href="http://www.freewarejava.com">Freewarejava</a><br>
- <a href="http://www.cooltext.com">Cool Text</a><br>
- <a href="http://www.google.com">Google.com</a>
</span>
</div>
</body>
</html>

seisuki
05-19-2005, 04:14 AM
Many thanks. I've figured it out :)