YUI Library Home

YUI Library Examples: Menu Family: MenuItem Configuration Properties

Menu Family: MenuItem Configuration Properties

This example demonstrates how to set MenuItem configuration properties when adding items to a Menu instance.

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.

Setting MenuItem configuration properties

A MenuItem has a number of configuration properties that affect its behavior and visual rendering. Any of these configuration properties can be set when adding an item to its parent Menu instance by defining them in an object that is passed to the Menu's addItem, insertItem, or addItems methods. This example ulitizes the following configuration properties: text, selected, disabled, url, and checked.

1/*
2    Instantiate a Menu:  The first argument passed to the constructor
3    is the id for the Menu element to be created, the second is an 
4    object literal of configuration properties.
5*/ 
6 
7var oMenu = new YAHOO.widget.Menu("basicmenu", { fixedcenter: true } ); 
8 
9 
10/*
11    Add items to the Menu instance by passing an array of object literals 
12    (each of which represents a set of YAHOO.widget.MenuItem 
13    configuration properties) to the "addItems" method.
14*/ 
15 
16oMenu.addItems([ 
17 
18        { text: "Selected MenuItem", selected: true }, 
19        { text: "Disabled MenuItem", disabled: true }, 
20        { text: "MenuItem With A URL", url: "http://www.yahoo.com" }, 
21        { text: "Checked MenuItem", checked: true } 
22 
23    ]); 
24 
25 
26/*
27    Since this Menu instance is built completely from script, call the 
28    "render" method passing in the DOM element that it should be 
29    appended to.
30*/ 
31 
32oMenu.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