This example demonstrates different ways to create and use a Menu Button.
With the inclusion of the optional Menu library, it is possible to create Buttons that incorporate a menu.
Menu Buttons can be created with or without existing HTML. In either case, create a Menu Button by setting the "type" configuration attribute to "menu" and the "menu" configuration attribute to one of the following values:
<div>
element used to create the menu. By default the menu will be created as an instance of YAHOO.widget.Overlay. If the default CSS class name for YAHOO.widget.Menu is applied to the <div>
element, it will be created as an instance of YAHOO.widget.Menu.<select>
element used to create the menu.<select>
element used to create the menu.Since the "menu" attribute can be set to the id of an existing <select>
element, a Menu Button can be used to collapse two HTML form controls (<input>
and <select>
) into one DHTML control. For example, consider the following HTML:
To instantiate a Menu Button, pass the id of the source element as the first argument to the Button's constructor. Set the "type" configuration attribute to "menu" and the "menu" configuration attribute to the id of the Button's corresponding <select>
element.
As a convenience, if the "type" attribute of the Button's source <input>
element was set to "submit", clicking on any MenuItem in the Button's Menu will automatically submit the Button's parent <form>
.
Add MenuItems to a Button's Menu using the Menu's
addItem
,
addItems
, and
insertItem
methods.
The following example adds two additional MenuItems to the first Button's Menu by
passing an array of object literals (each containing a set of
MenuItem configuration properties)
to the addItems
method.
Add event listeners to a Button's Menu using the Menu's
subscribe
method.
The following example adds a click
event listener to the Button's
Menu. The click
event listener displays the text label and value
of the MenuItem that was clicked.
Another way to create a Menu Button from markup is to begin with an
<input>
element and the markup format required for
Overlay:
To instantiate the Menu Button, pass the id of the source element as the first argument to the Button's constructor. Set the "type" configuration attribute to "menu" and the "menu" configuration attribute to the id or node reference of the HTML element to be used to create the Overlay:
Using an Overlay instance as a Menu Button's menu is useful when you need a simple container to house HTML content or another YUI widget, such as a Calendar or Color Picker.
It is also possible to create a Menu Button that utilizes Overlay completely from JavaScript. Begin by instantiating an Overlay. Then instantiate a Menu Button, setting its "type" configuration attribute to "menu" and its "menu" configuration attribute to the Overlay instance via an object literal passed as a single argument to the Button's constructor:
Another easy way to create a Menu Button from JavaScript is to set the "menu" configuration property to an array of MenuItem configuration properties.
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.
Note: Logging and debugging is currently turned off for this example.
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings