473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Adding an additional submenu to a collapsible/expandable menu

Hello All,

I am in the process of creating a 2/3 level collapsible/exspanible menu
(called "nav") where users can click on a category and have the
submenus appear beneath it and so on (allow users to click on another
category and have that expand).

Example:
>Corp Info
>>Corp Overview
Remote Sites
Departments
>>Human Resources
HR Staff
HR Forms
HR Etc.
Currently the code below allows the users to the drill down one level,
but I want it to be able to drill down an additional level. Any help
would be much appreciated.

Jonathan
Current Code:
************************************************** **************************
<style type="text/css">
#nav {width: 170px; height: 500px}
#nav, #nav ul, #nav li {margin: 0; padding: 0; list-style: none;}
#nav li {clear: both;}
#nav li b {display: block; height: 1.2em; color: white; background:
#000033; width: 100%; border-top: thin solid #CCCCCC; padding: 4px;}
#nav li ul li a {display: block; height: 1.2em; width: 100%; padding:
4px; text-decoration: none; color: black; background: #ededed;
border-top: thin solid #CCCCCC;}
#nav li ul li {font-size: 92%; border: #000033; background: none;}
#nav li ul li a:hover {color: #000033; background: orange; }
</style>

<script type="text/javascript">

function setCookie (name, value, path, domain, secure, expires)
{
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}

function getCookie (name)
{
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie (name, path, domain)
{
if (getCookie(name))
{
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
function addLoadEvent (func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
}
else
{
window.onload = function()
{
oldonload();
func();
}
}
}

</script>

<script type="text/javascript">

function menu_init ()
{
var menu = document.getElementById('nav');
var subs = menu.childNodes;

var j = 0;

for (var i=0 ; subs[i]; i++)
{
if (subs[i].tagName=='LI')
{
hs = subs[i].getElementsByTagName('B');
heading = hs[0];
ss = subs[i].getElementsByTagName('UL');
submenu = ss[0];

j++;

heading.onclick = function () { menu_toggle(this); };

if (getCookie('menu'+j)=='1')
submenu.style.display = 'block';
else if (getCookie('menu'+j)=='0')
submenu.style.display = 'none';
else if (j==1)
submenu.style.display = 'block';
else
submenu.style.display = 'none';
}
}
}

function menu_toggle (heading)
{
var section = heading.parentNode;
var submenus = section.getElementsByTagName('UL');
var submenu = submenus[0];

if (submenu.style.display=='none')
submenu.style.display = 'block';
else
submenu.style.display = 'none';

var j = 0;

var menu = document.getElementById('nav');
var subs = menu.childNodes;
for (var i=0 ; subs[i]; i++)
{
if (subs[i].tagName=='LI')
{
hs = subs[i].getElementsByTagName('B');
h = hs[0];
j++;

if (h==heading && submenu.style.display=='none')
setCookie('menu'+j, '0', '/');
else if (h==heading)
setCookie('menu'+j, '1', '/');
}
}
}

addLoadEvent(menu_init);

</script>

<!--[if IE 6]>
<script type="text/javascript">
// Suckerfish-like code to get B:hover working in IE 6.
function sucker_bold ()
{
bs = document.getElementById("nav").getElementsByTagNam e('B');
for (i=0; bs[i]; i++)
{
node = bs[i];
node.onmouseover=function() { this.className+=" over"; }
node.onmouseout=function() { this.className=this.className.replace("
over", ""); }
}
}
addLoadEvent(sucker_bold);
</script>
<![endif]-->

<DIV id=leftNavContainer>
<ul id="nav">
<li>
<b>Corporate Information</b>
<ul>
<li><a href="#">Corporate
Overview</a></li>
<li><a href="#">Remote Sites</a></li>
</ul>
</li>
<li>
<b>Departments</b>
<!--<ul>-->
<ul>
<li><a href="#">Human
Resources</a></li>
<!-- <li><a href="">HR Staff</a></li>
<li><a href="">HR Forms</a></li>
<li><a href="">Benefits</a></li>
</ul>-->
<li><a
href="#">Accounting</a></li>
<!-- <ul>
<li><a href="">Accounting
Staff</a></li>
<li><a href="">Organizational
Chart</a></li>
</ul>-->
</ul>
</li>

</ul>
</div>
************************************************** ************************************************** *****

Jan 5 '07 #1
5 5818
Jonathan wrote:
Currently the code below allows the users to the drill down one level,
but I want it to be able to drill down an additional level. Any help
would be much appreciated.
This looks startlingly like an old script of mine.

I've not tried it with three nav levels, but I *believe* the key is to
replace this:

subs = menu.childNodes();

with this:

subs = menu.getElementsByTagName('LI');

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 5 '07 #2
Toby Inkster wrote:
Jonathan wrote:
>Currently the code below allows the users to the drill down one level,
but I want it to be able to drill down an additional level. Any help
would be much appreciated.

This looks startlingly like an old script of mine.

I've not tried it with three nav levels, but I *believe* the key is to
replace this:

subs = menu.childNodes();

with this:

subs = menu.getElementsByTagName('LI');
I did up a demo for someone here recently that is a little more modern
for attaching the events and degrades to a fully expanded list if
JavaScript is disabled

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>template</title>
<style type="text/css">
#sm1,#sm2,#sm3 {
display: block;
}
</style>

<script type="text/javascript">
var buf='<style type="text/css">#sm1, #sm2, #sm3 { display: none;
}</style>';
document.write(buf);
</script>

<script type="text/javascript">

var menus=new Array('m1', 'm2', 'm3');

function toggle(e){
var me;
if(!e) var e=window.event;
if(e.target) me=e.target; // W3C
else if(e.srcElement) me=e.srcElement; // MSIE
if(me.nodeType == 3) me=me.parentNode; // Safari bug on elements
with TEXT

var id='s' + me.id;
var submenu=document.getElementById(id);
me.flag =! me.flag;
if(me.flag) submenu.style.display="block";
else submenu.style.display="none";
}

function initMenus(){
for( var i=0; i<menus.length; i++){
var menu=document.getElementById(menus[i]);

//alert('Init ' + menus[i] + ' which is ' + menu.tagName );

menu.flag=false;

if(menu.addEventListener){
menu.addEventListener('click', toggle, false);
}
else if(menu.attachEvent){ //MS IE support
menu.attachEvent('onclick', toggle);
}
}
}

// attach event after page loads
if( window.addEventListener ) {
window.addEventListener('load',initMenus,false); //legacy
} else if( document.addEventListener ) {
document.addEventListener('load',initMenus,false); //proper
} else if( window.attachEvent ) {
window.attachEvent("onload", initMenus); //IE only
}

</script>

</head>
<body>

<ul>
<li><a href="#" id="m1">Menu 1</a>
<ul id="sm1">
<li>Submenu 1.1</li>
<li>Submenu 1.2</li>
<li>Submenu 1.3</li>
</ul>
</li>
<li><a href="#" id="m2">Menu 2</a>
<ul id="sm2">
<li>Submenu 2.1</li>
<li>Submenu 2.2</li>
<li>Submenu 2.3</li>
<li>Submenu 2.4</li>
</ul>
</li>
<li><a href="#" id="m3">Menu 3</a>
<ul id="sm3">
<li>Submenu 3.1</li>
<li>Submenu 3.2</li>
</ul>
</li>
<ul>

</body>
</html>

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Jan 5 '07 #3
Jonathan,

Is there anything special that I need to do to view your example? I
saved your code (as an new html file) and attempted to view it to no
avail. What would you suggest me to do to view your example. Thanks
for your assistance.

Jonathan

Jonathan N. Little wrote:
Toby Inkster wrote:
Jonathan wrote:
Currently the code below allows the users to the drill down one level,
but I want it to be able to drill down an additional level. Any help
would be much appreciated.
This looks startlingly like an old script of mine.

I've not tried it with three nav levels, but I *believe* the key is to
replace this:

subs = menu.childNodes();

with this:

subs = menu.getElementsByTagName('LI');

I did up a demo for someone here recently that is a little more modern
for attaching the events and degrades to a fully expanded list if
JavaScript is disabled

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>template</title>
<style type="text/css">
#sm1,#sm2,#sm3 {
display: block;
}
</style>

<script type="text/javascript">
var buf='<style type="text/css">#sm1, #sm2, #sm3 { display: none;
}</style>';
document.write(buf);
</script>

<script type="text/javascript">

var menus=new Array('m1', 'm2', 'm3');

function toggle(e){
var me;
if(!e) var e=window.event;
if(e.target) me=e.target; // W3C
else if(e.srcElement) me=e.srcElement; // MSIE
if(me.nodeType == 3) me=me.parentNode; // Safari bug on elements
with TEXT

var id='s' + me.id;
var submenu=document.getElementById(id);
me.flag =! me.flag;
if(me.flag) submenu.style.display="block";
else submenu.style.display="none";
}

function initMenus(){
for( var i=0; i<menus.length; i++){
var menu=document.getElementById(menus[i]);

//alert('Init ' + menus[i] + ' which is ' + menu.tagName );

menu.flag=false;

if(menu.addEventListener){
menu.addEventListener('click', toggle, false);
}
else if(menu.attachEvent){ //MS IE support
menu.attachEvent('onclick', toggle);
}
}
}

// attach event after page loads
if( window.addEventListener ) {
window.addEventListener('load',initMenus,false); //legacy
} else if( document.addEventListener ) {
document.addEventListener('load',initMenus,false); //proper
} else if( window.attachEvent ) {
window.attachEvent("onload", initMenus); //IE only
}

</script>

</head>
<body>

<ul>
<li><a href="#" id="m1">Menu 1</a>
<ul id="sm1">
<li>Submenu 1.1</li>
<li>Submenu 1.2</li>
<li>Submenu 1.3</li>
</ul>
</li>
<li><a href="#" id="m2">Menu 2</a>
<ul id="sm2">
<li>Submenu 2.1</li>
<li>Submenu 2.2</li>
<li>Submenu 2.3</li>
<li>Submenu 2.4</li>
</ul>
</li>
<li><a href="#" id="m3">Menu 3</a>
<ul id="sm3">
<li>Submenu 3.1</li>
<li>Submenu 3.2</li>
</ul>
</li>
<ul>

</body>
</html>

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Jan 5 '07 #4
Jonathan wrote:
Jonathan,

Is there anything special that I need to do to view your example? I
saved your code (as an new html file) and attempted to view it to no
avail. What would you suggest me to do to view your example. Thanks
for your assistance.
Nope only that your have JavaScript enabled and DOM1 browser. Mind you
this just just a strip down example. When first loaded should only see
the three main menu items. Click one on and the submenu should expand...

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Jan 5 '07 #5
[snip]
> var buf='<style type="text/css">#sm1, #sm2, #sm3 { display: none;
}</style>';
watch for line wrap
[snip]
> if(me.nodeType == 3) me=me.parentNode; // Safari bug on
elements with TEXT
watch for line wrap
[snip]
Jonathan <jo**************@gmail.comwrote:
news: 11**********************@s34g2000cwa.googlegroups. com
Jonathan,

Is there anything special that I need to do to view your example? I
saved your code (as an new html file) and attempted to view it to no
avail. What would you suggest me to do to view your example. Thanks
for your assistance.
Fix the line wrap and perhaps it will work for you.

--
BootNic Friday, January 05, 2007 11:54 PM

Optimism and humor are the grease and glue of life. Without both of
them we would never have survived our captivity.
*Philip Butler, Vietnam POW*

Jan 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bill drescher | last post by:
The menu at: http://www.alsacreations.com/articles/deroulant/cssmenu.htm does exactly what I want, except: If you leave a menu (and submenu) by going _down_ to the next menu the first does not...
1
by: Benoit | last post by:
Here is a quick view of what my program is doing: I am using a system hook to add a menu inside the system menu of my Internet Explorer. Everything seems to work fine for now , my submenu is...
0
by: Claire | last post by:
When I run the following code through on the first round, my sub-menu (branched off mnuOpenPorts menuitem) shows a nice list of ports. When it gets run through on the 2nd pass, the sub menu...
4
by: Claire | last post by:
Sorry Ive added this twice (sortof) but if I'd added an addendum to the first one then this would probably have been ignored. This problem affects a ContextMenu attached to a NotifyIcon object. I...
1
by: mike | last post by:
The JavaScript below works fine for expandable/collapsible menus in IE, but Firefox 1.5 complains: "Error: loc.parentNode.nextSibling has no properties" and highlights the line...
2
by: pantagruel | last post by:
Hi, I'm wondering if CSS support in IE is such that one could make an expandable collapsible vertical menu - such as in a TOC - without using Javascript that will work in at least IE 6. IE 5...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
by: bhosalevivek | last post by:
I did this code from Dynamic Drive DHTML. it's working properly, but now I need submenu for "Submenu 1.2", means when I take mouse on "Submenu 1.2" I need to display "Submenu 1.2.1". I try to do...
1
by: zacks | last post by:
I have figured out how to add custom menu items to various buildin context menus by looking at a project on CodeProject.com and then doing some registry searches. But I can't find how to add a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.