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.

Menu Family Examples:

More Menu Family Resources:

Copyright © 2008 Yahoo! Inc. All rights reserved.

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