YUI Library Examples: Menu Family: Grouped Menu Items Using Markup

Menu Family: Grouped Menu Items Using Markup

This example demonstrates how to group 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

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.

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