YUI Library Home

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

Menu Family: Multi-tiered Menu From JavaScript

This example demonstrates how to create a multi-tiered menu using nothing but JavaScript.

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 JavaScript

To create a Menu with no pre-existing markup on the page, call the Menu constructor (YAHOO.widget.Menu) passing the id of the Menu element to be created as the first argument.

Next, define an array of MenuItem configuration properties. Add a submenu to a MenuItem by setting the MenuItem instance's "submenu" configuration property to an object literal that specifies values for both the "id" and "itemdata" properties. The "id" property specifies the id of the Menu to be created, while the "itemdata" property accepts an array of MenuItem configuration properties, each of which is used to configure the individual items in the Menu. Once defined, pass the array of MenuItem configuration properties to the addItems method of Menu.

Finally, it is necessary to call the render method passing the id of, or reference to the element the Menu should be appended to. Just as with creating a Menu hierarchy via existing HTML, it is only necessary to instantiate and render the root Menu as each submenu is automatically instantiated and rendered with the root Menu as a convenience.

1/*
2     Define an array of object literals, each containing 
3     the configuration properties necessary to create a MenuItem and 
4     its corresponding submenu.
5*/ 
6 
7var aItems = [ 
8 
9    { 
10        text: "Communication",  
11        url: "#communication",  
12        submenu: {  
13         
14            id: "communication",  
15            itemdata: [  
16     
17                { text: "360", url: "http://360.yahoo.com" }, 
18                { text: "Alerts", url: "http://alerts.yahoo.com" }, 
19                { text: "Avatars", url: "http://avatars.yahoo.com" }, 
20                { text: "Groups", url: "http://groups.yahoo.com " }, 
21                { text: "Internet Access", url: "http://promo.yahoo.com/broadband" }, 
22                { 
23                    text: "PIM",  
24                    url: "#pim",  
25                    submenu: { 
26                        id: "pim",  
27                        itemdata: [ 
28 
29                            { text: "Yahoo! Mail", url: "http://mail.yahoo.com" }, 
30                            { text: "Yahoo! Address Book", url: "http://addressbook.yahoo.com" }, 
31                            { text: "Yahoo! Calendar",  url: "http://calendar.yahoo.com" }, 
32                            { text: "Yahoo! Notepad", url: "http://notepad.yahoo.com" } 
33 
34                        ]  
35                    } 
36                 
37                },  
38                { text: "Member Directory", url: "http://members.yahoo.com" }, 
39                { text: "Messenger", url: "http://messenger.yahoo.com" }, 
40                { text: "Mobile", url: "http://mobile.yahoo.com" }, 
41                { text: "Flickr Photo Sharing", url: "http://www.flickr.com" } 
42     
43            ]  
44         
45        }  
46     
47    }, 
48 
49    { 
50        text: "Shopping",  
51        url: "http://shopping.yahoo.com",  
52        submenu: { 
53            id: "shopping",  
54            itemdata: [ 
55 
56                { text: "Auctions", url: "http://auctions.shopping.yahoo.com" }, 
57                { text: "Autos", url: "http://autos.yahoo.com" }, 
58                { text: "Classifieds", url: "http://classifieds.yahoo.com" }, 
59                { text: "Flowers & Gifts", url: "http://shopping.yahoo.com/b:Flowers%20%26%20Gifts:20146735" }, 
60                { text: "Real Estate", url: "http://realestate.yahoo.com" }, 
61                { text: "Travel", url: "http://travel.yahoo.com" }, 
62                { text: "Wallet", url: "http://wallet.yahoo.com" }, 
63                { text: "Yellow Pages", url: "http://yp.yahoo.com" } 
64     
65            ]  
66        }  
67    }, 
68 
69    { 
70        text: "Entertainment",  
71        url: "http://entertainment.yahoo.com",  
72        submenu: {  
73            id: "entertainment",  
74            itemdata: [ 
75     
76                { text: "Fantasy Sports", url: "http://fantasysports.yahoo.com" }, 
77                { text: "Games", url: "http://games.yahoo.com" }, 
78                { text: "Kids", url: "http://www.yahooligans.com" }, 
79                { text: "Music", url: "http://music.yahoo.com" }, 
80                { text: "Movies", url: "http://movies.yahoo.com" }, 
81                { text: "Radio", url: "http://music.yahoo.com/launchcast" }, 
82                { text: "Travel", url: "http://travel.yahoo.com" }, 
83                { text: "TV", url: "http://tv.yahoo.com" }               
84            ]  
85        }  
86    }, 
87 
88    { 
89        text: "Information",  
90        url: "#information",  
91        submenu: { 
92            id: "information",  
93            itemdata: [ 
94     
95                { text: "Downloads", url: "http://downloads.yahoo.com" }, 
96                { text: "Finance", url: "http://finance.yahoo.com" }, 
97                { text: "Health", url: "http://health.yahoo.com" }, 
98                { text: "Local", url: "http://local.yahoo.com" }, 
99                { text: "Maps & Directions", url: "http://maps.yahoo.com" }, 
100                { text: "My Yahoo!", url: "http://my.yahoo.com" }, 
101                { text: "News", url: "http://news.yahoo.com" }, 
102                { text: "Search", url: "http://search.yahoo.com" }, 
103                { text: "Small Business", url: "http://smallbusiness.yahoo.com" }, 
104                { text: "Weather", url: "http://weather.yahoo.com" } 
105     
106            ] 
107        }  
108    }             
109];                 
110 
111 
112/*
113    Instantiate a Menu:  The first argument passed to the constructor
114    is the id for the Menu element to be created, the second is an 
115    object literal of configuration properties.
116*/ 
117 
118var oMenu = new YAHOO.widget.Menu("productsandservices", { fixedcenter: true }); 
119 
120 
121/*
122    Add items to the Menu instance by passing an array of object literals 
123    (each of which represents a set of YAHOO.widget.MenuItem 
124    configuration properties) to the "addItems" method.
125*/ 
126 
127oMenu.addItems(aItems); 
128 
129 
130/*
131    Since this Menu instance is built completely from script, call the 
132    "render" method passing in the DOM element that it should be 
133    appended to.
134*/ 
135 
136oMenu.render("rendertarget"); 
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