saitarion Posted May 16, 2011 Share Posted May 16, 2011 Hello all!I have edited code from an external source to fit my needs.Everything works with the exception of the following large problem.The problem is that the value of SwitchMenu(id) is either a value of 'sub1', 'sub2', 'sub3' or nothing.I need the SwitchMenu(id) to have a default value of 'sub1'. Currently if the var 'sub1' is selected and 'sub1' is selected again, the menu dissapears as the value becomes nothing. I need it to always have a value.Section of JavaScript that manages the (id) value. function SwitchMenu(id) { if(document.getElementById) { var el = document.getElementById(id); var ar = document.getElementById("menu-box").getElementsByTagName("div"); if(el.style.display != "block") { for (var i=0; i<ar.length; i++) { if (ar[i].className=="menu-hide") ar[i].style.display = "none"; } el.style.display = "block"; } else { el.style.display = "none"; } }} Below is an extract of code for the menu. <!-- sub1 --><div class="menu-hide" id="sub1"> <div class="mini-menu content right"> <div class="mini-menu"> <h4>all</h4> </div> <div class="mini-menu" onclick="SwitchMenu('sub2')"> <h4>sub-menu-1</h4> </div> <div class="mini-menu" onclick="SwitchMenu('sub3')"> <h4>sub-menu-1</h4> </div> </div> <div class="content"> <div class="menu"> <h3><a href="index.html">Home</a></h3> </div> <div class="menu"> <h3><a href="a.html">A</a></h3> </div> <div class="menu"> <h3><a href="b.html">B</a></h3> </div> </div></div><!-- sub2 --><div class="menu-hide" id="sub2"> <div class="mini-menu content right"> <div class="mini-menu" onclick="SwitchMenu('sub1')"> <h4>all</h4> </div> <div class="mini-menu"></div> <h4>sub-menu-1</h4> </div> <div class="mini-menu" onclick="SwitchMenu('sub3')"> <h4>sub-menu-2</h4> </div> </div> <div class="content"> <div class="menu"> <h3><a href="index.html">Home</a></h3> </div> <div class="menu"> <h3><a href="a.html">A</a></h3> </div> <div class="menu"> <h3><a href="b.html">B</a></h3> </div> </div></div><!-- sub3 --><div class="menu-hide" id="sub3"> <div class="mini-menu content right"> <div class="mini-menu" onclick="SwitchMenu('sub1')"> <h4>all</h4> </div> <div class="mini-menu" onclick="SwitchMenu('sub'2)"> <h4>sub-menu-1</h4> </div> <div class="mini-menu"> <h4>sub-menu-2</h4> </div> </div> <div class="content"> <div class="menu"> <h3><a href="index.html">Home</a></h3> </div> <div class="menu"> <h3><a href="a.html">A</a></h3> </div> <div class="menu"> <h3><a href="b.html">B</a></h3> </div> </div></div> I have been able to test the menu by having the index.html body include onload="SwitchMenu('sub1')" so it looks like this; <body onload="SwitchMenu('sub1')"> This has allowed me to go through the menu and ensure it works, however if the index.html file is loaded, it obviously makes the menu disappear.Any and all help would be greatly appreciated!!! Link to comment Share on other sites More sharing options...
justsomeguy Posted May 16, 2011 Share Posted May 16, 2011 If you're trying to set the id variable to a default value if it's empty, you can do this: function SwitchMenu(id) { if ((typeof id) == 'undefined' || id == '') id = 'sub1'; Link to comment Share on other sites More sharing options...
saitarion Posted May 17, 2011 Author Share Posted May 17, 2011 Brilliant! Thankyou. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.