YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Button Control: Menu Buttons

# Button Control: Menu Buttons

This example demonstrates different ways to create a Menu Button.

View example in new window.

Menu Buttons
From Markup
Menu Button 3 Menu
From JavaScript

## Creating Menu Buttons

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:

- Object specifying a YAHOO.widget.Menu instance.
- Object specifying a YAHOO.widget.Overlay instance.
- String specifying the id attribute of the `<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.
- String specifying the id attribute of the `<select/>` element used to create the menu.
- Object specifying the `<select/>` element used to create the menu.
- Array of object literals, each representing a set of YAHOO.widget.MenuItem configuration properties.
- Array of strings representing the text labels for each item in 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:

<input type="submit" id="menubutton1" name="menubutton1_button" value="Menu Button 1"> <select id="menubutton1select" name="menubutton1select"> <option value="0">One</option> <option value="1">Two</option> <option value="2">Three</option> </select> <input type="button" id="menubutton2" name="menubutton2_button" value="Menu Button 2"> <select id="menubutton2select" name="menubutton2select"> <option value="0">One</option> <option value="1">Two</option> <option value="2">Three</option> </select>

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.

var oMenuButton1 = new YAHOO.widget.Button("menubutton1", { type: "menu", menu: "menubutton1select" }); var oMenuButton2 = new YAHOO.widget.Button("menubutton2", { type: "menu", menu: "menubutton2select" });

Please note: If the source `<input/>` element's type was "submit," the Menu Button will automatically submit its parent form when the user clicks or presses the button or chooses an option from the its menu.

Another way to create a Menu Button from markup is to begin with an `<input/>` element and the markup format required for YAHOO.widget.Overlay:

<input type="button" id="menubutton3" name="menubutton3_button" value="Menu Button 3"> <div id="menubutton3menu" class="yui-overlay"> <div class="bd">Menu Button 3 Menu</div> </div>

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: var oMenuButton3 = new YAHOO.widget.Button("menubutton3", { type: "menu", menu: "menubutton3menu" });

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 YAHOO.widget.Overlay completely from JavaScript. Simply instantiate and render an Overlay instance. 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:

/* Search for an element to place the Menu Button into via the Event utilities "onContentReady" method */ YAHOO.util.Event.onContentReady("menubuttonsfromjavascript", function () { // Instantiate an Overlay instance var oOverlay = new YAHOO.widget.Overlay("menubutton5menu", { visible: false }); oOverlay.setBody("Menu Button 5 Menu"); // Instantiate a Menu Button var oMenuButton5 = new YAHOO.widget.Button({ type: "menu", label: "Menu Button 5", menu: oOverlay }); /* Append the Menu Button and Overlay to the element with the id of "menubuttonsfromjavascript" */ oMenuButton5.appendTo(this); oOverlay.render(this); });

Another easy way to create a Menu Button from JavaScript is to set the "menu" configuration property to an array of YAHOO.widget.MenuItem configuration properties.

// "click" event handler for each item in the Button's menu function onMenuItemClick(p_sType, p_aArgs, p_oItem) { oMenuButton4.set("label", p_oItem.cfg.getProperty("text")); } // Create an array of YAHOO.widget.MenuItem configuration properties var aMenuButton4Menu = [ { text: "one", value: 1, onclick: { fn: onMenuItemClick } }, { text: "two", value: 2, onclick: { fn: onMenuItemClick } }, { text: "three", value: 3, onclick: { fn: onMenuItemClick } } ]; /* Instantiate a Menu Button using the array of YAHOO.widget.MenuItem configuration properties as the value for the "menu" configuration attribute. */ var oMenuButton4 = new YAHOO.widget.Button({ type: "menu", label: "one", name: "mymenubutton", menu: aMenuButton4Menu, container: "menubuttonsfromjavascript" });

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Button Control Examples:

- Push Buttons
- Link Buttons
- Checkbox Buttons
- Radio Buttons
- Submit Buttons
- Reset Buttons
- Menu Buttons
- Split Buttons
- Simple Calendar Menu Button
- Calendar Menu Button with Date on Button Face
- Color Picker Button
- Fixed Width Menu Button
- Glowing Button
- Slider Button
- Using the Button ARIA Plugin
- Combo Box (included with examples for AutoComplete Control)
- Showing, Hiding, and Reordering Columns. (included with examples for DataTable Control)
- Getting CSS Style Sheets (included with examples for Get Utility)
- Complex Application (included with examples for Layout Manager)

### More Button Control Resources:

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

Copyright © 2008 Yahoo! Inc. All rights reserved.

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