YUI Library Home

YUI Library Examples: Menu Family: Multi-tiered Menu From Markup

Menu Family: Multi-tiered Menu From Markup

This example demonstrates how to create a multi-tiered menu using existing markup on the page.

Note: By default clicking outside of a Menu instance will hide it. Additionally, MenuItem instances without a submenu or a URL to navigate to will hide their parent Menu instance when clicked. Click the "Show Menu" button below to make the Menu instance visible if it is hidden.

Creating a Menu with submenus from existing markup

The markup for a Menu control follows that of YAHOO.widget.Module, with its body element (<div class="bd">) containing a list element (<ul>).

Each item in a Menu is represented by a list item element (<li class="yuimenuitem">), each of which has a label (<a class="yuimenuitemlabel">) that can contain plain text or HTML.

In addition to a label, each MenuItem instance can also contain a submenu. To add a submenu via markup, simply nest the Menu markup inside the (<li class="yuimenuitem">) element representing a MenuItem instance.

1<div id="productsandservices" class="yuimenu"
2    <div class="bd"
3        <ul class="first-of-type"
4            <li class="yuimenuitem"
5             
6                <class="yuimenuitemlabel" href="#communication"
7                    Communication 
8                </a> 
9 
10                <!-- A submenu --> 
11 
12                <div id="communication" class="yuimenu"
13                    <div class="bd"
14                        <ul> 
15 
16                        <!-- Items for the submenu go here --> 
17 
18                        </ul> 
19                    </div> 
20                </div>                     
21             
22            </li> 
23            <li class="yuimenuitem"
24             
25                <class="yuimenuitemlabel" href="http://shopping.yahoo.com"
26                    Shopping 
27                </a> 
28 
29                <!-- A submenu --> 
30 
31                <div id="shopping" class="yuimenu"
32                    <div class="bd">                     
33                        <ul> 
34 
35                        <!-- Items for the submenu go here --> 
36 
37                        </ul> 
38                    </div> 
39                </div>                     
40             
41            </li> 
42            <li class="yuimenuitem"
43             
44                <class="yuimenuitemlabel" href="http://entertainment.yahoo.com"
45                    Entertainment 
46                </a> 
47 
48                <!-- A submenu --> 
49 
50                <div id="entertainment" class="yuimenu"
51                    <div class="bd">                     
52                        <ul> 
53 
54                        <!-- Items for the submenu go here --> 
55 
56                        </ul>                     
57                    </div> 
58                </div>                                         
59             
60            </li> 
61            <li class="yuimenuitem"
62             
63                <class="yuimenuitemlabel" href="#information"
64                    Information 
65                </a> 
66 
67                <!-- A submenu --> 
68 
69                <div id="information" class="yuimenu"
70                    <div class="bd">                                         
71                        <ul> 
72 
73                        <!-- Items for the submenu go here --> 
74 
75                        </ul>                     
76                    </div> 
77                </div>                                         
78             
79            </li> 
80        </ul>             
81    </div> 
82</div> 
view plain | print | ?

To instantiate a Menu hierarchy based on existing HTML, pass the id of its corresponding root HTML element (in this case "productsandservices") to the Menu constructor (YAHOO.widget.Menu) then call the render method with no arguments. There is no need to instantiate and render submenus; as a convenience all submenus are automatically instantiated and rendered with the root Menu.

1/*
2    Instantiate the menu and corresponding submenus. The first argument passed 
3    to the constructor is the id of the element in the DOM that represents 
4    the menu; the second is an object literal representing a set of 
5    configuration properties for the menu.
6*/  
7 
8var oMenu = new YAHOO.widget.Menu("productsandservices", { fixedcenter: true }); 
9 
10 
11/*
12     Call the "render" method with no arguments since the 
13     markup for this Menu instance is already exists in the page.
14*/ 
15 
16oMenu.render(); 
view plain | print | ?

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Menu Family Examples:

More Menu Family Resources:

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings