In a previous article, I described an easy method to create an expanding menu similar to Windows Explorer, but with only one level of sub-menus. This article provides code for a similar expanding menu, except with two levels of sub-menus.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Easy Three-level Expanding Menu Code

In a previous article, I described an easy method to create an expanding menu similar to Windows Explorer, but with only one level of sub-menus. This article provides code for a similar expanding menu, except with two levels of sub-menus.

This example requires three blocks of code, html to create the menu, CSS to style the menu and add mouse over effects, and Java Script to toggle the menu elements display properties which causes them to expand and unexpand. I promise you that all this code is extremely simple and I'll explain it in excruciating detail. First let's start with a basic description of the html.

Shown below is an example of code for a folder containing two files.

Expanding menu

<a class="folder" href="#" onclick="toggleMenu('s1')">
<img id="is1" align="left" border="0" src="closed.gif">Sub Menu 1</a><br />

<div id="s1" style="display:none; margin-left:16px;">
  <a class="menulink" href="#">Link Text</a><br />
  <a class="menulink" href="#">Link Text</a><br />
</div>

Contained Within the bottom div are two links with class="menulink". These represent links to files within a folder. In these two links, you would edit Link Text to your link's text, and you would replace the # character with the complete or relative path to your destination page. You can simply cut-and paste lines to add more links, or delete a line to remove a link.

• Note that I applied an inline style attribute to this div. Some designers would say this is high treason against the basic principles of CSS. Since there are only two rules in this style definition, I wouldn't save much bandwidth by using a class attribute.

Immediately above the div is a link who's onclick event executes a function named toggleMenu passing the id of the above mentioned div to the function. This function toggles the display property of the div. We'll describe this function a little later. You would change the text Sub Menu 1 to the name of your folder.

Shown below is an example of code for a folder containing a subfolder which contains two files.

Expanding menu

<a class="folder" href="#" onclick="toggleMenu('t1')">
<img id="it1" align="left" border="0" src="closed.gif">Top Menu 1</a><br />

<div id="t1" style="display:none; margin-left:16px;">

  <a class="folder" href="#" onclick="toggleMenu('s1')">
  <img id="is1" align="left" border="0" src="closed.gif">Sub Menu 1</a><br />

  <div id="s1" style="display:none; margin-left:16px;">
    <a class="menulink" href="#">Link Text</a><br />
    <a class="menulink" href="#">Link Text</a><br />
  </div>

</div>

Can you see the code we described previously nested inside the new div? Immediately above the new div is a link who's onclick event calls the toggleMenu function, passing the id of the new div. This causes the new div and its entire contents to toggle between visible (display:block;) and invisible (display:hidden;). In other words to expand or unexpand.

Web Design Sections

RSS Feed RSS Feed

Cascading Style Sheets
Understanding CSS Selectors
The CSS Box Model
Understanding CSS Positioning
Add Style to Your Blockquotes
Display Overlapping Images on Your Webpage
Add Drop Shadows to Your Pictures
Style Your Imagemap Tooltips
How to Style a List
How to Style a Table
How to Change the Mouse Pointer
How to Use a Starburst on Your Web Page
How to Use a Pull Quote
Easy Scrollable Area Code
Easy CSS Buttons
Create CSS Button Rollovers
Create Custom Horizontal Rules
Easy CSS Popup Windows
Easy Visual Effects to Spice Up Your Webpage
Spice Up Your Web Forms with Color and Graphics
Setting a Larger First Letter
Make a Fixed-width Variable-height Round Cornered Box
HTML, XHTML, and CSS Bible
Web Designer's Reference
Pro CSS Techniques
Code for Horizontal Drop-down Menu Bar
Easy Rollover Menu Code
Easy CSS Tabbed Navigation
Easier Expanding Menu Code
Easy Three-level Expanding Menu Code
Easy Floating Menu Code


Your Career Quiz
[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269