YUI Library Home

Site Search (YDN & YUIBlog):

# 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.

View example in new window.

- Communication
  - 360°
  - Alerts
  - Avatars
  - Groups
  - Internet Access
  - PIM
    - Yahoo! Mail
    - Yahoo! Address Book
    - Yahoo! Calendar
    - Yahoo! Notepad
  - Member Directory
  - Messenger
  - Mobile
  - Flickr Photo Sharing
- Shopping
  - Auctions
  - Autos
  - Classifieds
  - Flowers & Gifts
  - Real Estate
  - Travel
  - Wallet
  - Yellow Pages
- Entertainment
  - Fantasy Sports
  - Games
  - Kids
  - Music
  - Movies
  - Radio
  - Travel
  - TV
- Information
  - Downloads
  - Finance
  - Health
  - Local
  - Maps & Directions
  - My Yahoo!
  - News
  - Search
  - Small Business
  - Weather

## 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.

<div id="productsandservices" class="yuimenu"> <div class="bd"> <ul class="first-of-type"> <li class="yuimenuitem"> <a class="yuimenuitemlabel" href="#communication"> Communication </a> <!-- A submenu --> <div id="communication" class="yuimenu"> <div class="bd"> <ul> <!-- Items for the submenu go here --> </ul> </div> </div> </li> <li class="yuimenuitem"> <a class="yuimenuitemlabel" href="http://shopping.yahoo.com"> Shopping </a> <!-- A submenu --> <div id="shopping" class="yuimenu"> <div class="bd"> <ul> <!-- Items for the submenu go here --> </ul> </div> </div> </li> <li class="yuimenuitem"> <a class="yuimenuitemlabel" href="http://entertainment.yahoo.com"> Entertainment </a> <!-- A submenu --> <div id="entertainment" class="yuimenu"> <div class="bd"> <ul> <!-- Items for the submenu go here --> </ul> </div> </div> </li> <li class="yuimenuitem"> <a class="yuimenuitemlabel" href="#information"> Information </a> <!-- A submenu --> <div id="information" class="yuimenu"> <div class="bd"> <ul> <!-- Items for the submenu go here --> </ul> </div> </div> </li> </ul> </div> </div>

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.

/* Instantiate the menu and corresponding submenus. The first argument passed to the constructor is the id of the element in the DOM that represents the menu; the second is an object literal representing a set of configuration properties for the menu. */ var oMenu = new YAHOO.widget.Menu("productsandservices", { fixedcenter: true }); /* Call the "render" method with no arguments since the markup for this Menu instance is already exists in the page. */ oMenu.render();

## 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:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2009 Yahoo! Inc. All rights reserved.

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