YUI Library Home

YUI Library Examples: Button Control: Menu Buttons

Button Control: Menu Buttons

This example demonstrates different ways to create and use a Menu Button.

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.

Creating Menu Buttons Using Existing Markup

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:

1<input type="submit" id="menubutton1" name="menubutton1_button" value="Menu Button 1"
2<select id="menubutton1select" name="menubutton1select"
3    <option value="0">One</option> 
4    <option value="1">Two</option> 
5    <option value="2">Three</option>                 
6</select> 
7 
8<input type="button" id="menubutton2" name="menubutton2_button" value="Menu Button 2"
9<select id="menubutton2select" name="menubutton2select"
10    <option value="0">One</option> 
11    <option value="1">Two</option> 
12    <option value="2">Three</option>                 
13</select> 
view plain | print | ?

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.

1var oMenuButton1 = new YAHOO.widget.Button("menubutton1", {  
2                                        type: "menu",  
3                                        menu: "menubutton1select" }); 
4 
5var oMenuButton2 = new YAHOO.widget.Button("menubutton2", {  
6                                        type: "menu",  
7                                        menu: "menubutton2select" }); 
view plain | print | ?

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

Adding MenuItems To A Button's Menu

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.

1//  "render" event handler for the Button's Menu 
2 
3var onMenuRender = function (p_sType, p_aArgs) { 
4 
5    this.addItems([ 
6 
7        { text: "Four", value: 4 }, 
8        { text: "Five", value: 5 } 
9 
10    ]); 
11     
12}; 
13 
14 
15//  Add some additional MenuItems to the Button's Menu once it has been rendered 
16 
17oMenuButton1.getMenu().subscribe("render", onMenuRender); 
view plain | print | ?

Adding Event Listeners To A Button's Menu

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.

1//  Click event listener for the second Button's Menu instance 
2 
3var onMenuClick = function (p_sType, p_aArgs) { 
4 
5    var oEvent = p_aArgs[0],    //  DOM event 
6        oMenuItem = p_aArgs[1]; //  MenuItem instance that was the target of the event 
7 
8    if (oMenuItem) { 
9        alert("[MenuItem Properties] text: " + oMenuItem.cfg.getProperty("text") + ", value: " + oMenuItem.value); 
10    } 
11 
12}; 
13 
14 
15//  Add a "click" event listener for the Button's Menu 
16 
17oMenuButton2.getMenu().subscribe("click", onMenuClick); 
view plain | print | ?

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

1<input type="button" id="menubutton3" name="menubutton3_button" value="Menu Button 3"
2<div id="menubutton3menu" class="yui-overlay"
3    <div class="bd">Menu Button 3 Menu</div> 
4</div> 
view plain | print | ?

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:

1var oMenuButton3 = new YAHOO.widget.Button("menubutton3", {  
2                                        type: "menu",  
3                                        menu: "menubutton3menu" }); 
view plain | print | ?

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.

Creating Menu Buttons Using JavaScript

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:

1//  Search for an element to place the Menu Button into via the  
2//  Event utility's "onContentReady" method 
3 
4YAHOO.util.Event.onContentReady("menubuttonsfromjavascript"function () { 
5 
6    //  Create two Buttons without using existing markup 
7 
8 
9    // Instantiate an Overlay instance 
10 
11    var oOverlay = new YAHOO.widget.Overlay("menubutton4menu", { visible: false }); 
12 
13    oOverlay.setBody("Menu Button 4 Menu"); 
14 
15 
16    // Instantiate a Menu Button 
17 
18    var oMenuButton4 = new YAHOO.widget.Button({ type: "menu", label: "Menu Button 4", menu: oOverlay, container: this }); 
19 
20}); 
view plain | print | ?

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

1//  Search for an element to place the Menu Button into via the  
2//  Event utility's "onContentReady" method 
3 
4YAHOO.util.Event.onContentReady("menubuttonsfromjavascript"function () { 
5 
6    //  "click" event handler for each item in the Button's menu 
7 
8    var onMenuItemClick = function (p_sType, p_aArgs, p_oItem) { 
9         
10        var sText = p_oItem.cfg.getProperty("text"); 
11         
12        alert("[MenuItem Properties] text: " + sText + ", value: " + p_oItem.value); 
13         
14        oMenuButton5.set("label", sText);            
15 
16    }; 
17 
18 
19    //  Create an array of YAHOO.widget.MenuItem configuration properties 
20 
21    var aMenuButton5Menu = [ 
22 
23        { text: "One", value: 1, onclick: { fn: onMenuItemClick } }, 
24        { text: "Two", value: 2, onclick: { fn: onMenuItemClick } }, 
25        { text: "Three", value: 3, onclick: { fn: onMenuItemClick } } 
26 
27    ]; 
28 
29 
30    //  Instantiate a Menu Button using the array of YAHOO.widget.MenuItem  
31    //  configuration properties as the value for the "menu"   
32    //  configuration attribute. 
33 
34    var oMenuButton5 = new YAHOO.widget.Button({ type: "menu", label: "One", name: "mymenubutton", menu: aMenuButton5Menu, container: this }); 
35 
36});        
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.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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