Yahoo! Developer Network Home - Help

YUI Library Examples: Menu Family: Grouped Menu Items With Titles From Markup

Menu Family: Grouped Menu Items With Titles From Markup

This example demonstrates how to title groups of MenuItem instances.

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 from existing markup with items organized into groups with titles

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.

The body of a Menu instance can house many <ul> elements to organize related items into groups. Each group can have a title by preceeding its <ul> with a <h6> element.

1<div id="menuwithgroups" class="yuimenu"
2    <div class="bd"
3        <h6 class="first-of-type">Yahoo! PIM</h6> 
4        <ul class="first-of-type"
5            <li class="yuimenuitem"
6                <class="yuimenuitemlabel" href="http://mail.yahoo.com"
7                    Yahoo! Mail 
8                </a> 
9            </li> 
10            <li class="yuimenuitem"
11                <class="yuimenuitemlabel" href="http://addressbook.yahoo.com"
12                    Yahoo! Address Book 
13                </a> 
14            </li> 
15            <li class="yuimenuitem"
16                <class="yuimenuitemlabel" href="http://calendar.yahoo.com"
17                    Yahoo! Calendar 
18                </a> 
19            </li> 
20            <li class="yuimenuitem"
21                <class="yuimenuitemlabel" href="http://notepad.yahoo.com"
22                    Yahoo! Notepad 
23                </a> 
24            </li> 
25        </ul> 
26        <h6>Yahoo! Search</h6> 
27        <ul> 
28            <li class="yuimenuitem"
29                <class="yuimenuitemlabel" href="http://local.yahoo.com"
30                    Yahoo! Local 
31                </a> 
32            </li> 
33            <li class="yuimenuitem"
34                <class="yuimenuitemlabel" href="http://maps.yahoo.com"
35                    Yahoo! Maps 
36                </a> 
37            </li> 
38            <li class="yuimenuitem"
39                <class="yuimenuitemlabel" href="http://travel.yahoo.com"
40                    Yahoo! Travel 
41                </a> 
42            </li> 
43            <li class="yuimenuitem"
44                <class="yuimenuitemlabel" href="http://shopping.yahoo.com"
45                    Yahoo! Shopping 
46                </a> 
47            </li> 
48        </ul> 
49        <h6>Yahoo! Communications</h6> 
50        <ul> 
51            <li class="yuimenuitem"
52                <class="yuimenuitemlabel" href="http://messenger.yahoo.com"
53                    Yahoo! Messenger 
54                </a> 
55            </li> 
56            <li class="yuimenuitem"
57                <class="yuimenuitemlabel" href="http://360.yahoo.com"
58                    Yahoo! 360 
59                </a> 
60            </li> 
61            <li class="yuimenuitem"
62                <class="yuimenuitemlabel" href="http://groups.yahoo.com"
63                    Yahoo! Groups 
64                </a> 
65            </li> 
66            <li class="yuimenuitem"
67                <class="yuimenuitemlabel" href="http://www.flickr.com"
68                    Flickr Photo Sharing 
69                </a> 
70            </li> 
71        </ul> 
72    </div> 
73</div> 
view plain | print | ?

To instantiate a Menu based on existing HTML, pass the id of its corresponding HTML element (in this case "basicmenu") to the Menu constructor (YAHOO.widget.Menu) then call the render method with no arguments.

1/*
2     Instantiate a Menu:  The first argument passed to the 
3     constructor is the id of the element in the page 
4     representing the Menu; the second is an object literal 
5     of configuration properties.
6*/ 
7 
8var oMenu = new YAHOO.widget.Menu("menuwithgroups", { 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 | ?

Menu Family Examples:

More Menu Family Resources:

Copyright © 2008 Yahoo! Inc. All rights reserved.

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