This example demonstrates different ways to create 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:
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.
1 | var oMenuButton1 = new YAHOO.widget.Button("menubutton1", { |
2 | type: "menu", |
3 | menu: "menubutton1select" }); |
4 | |
5 | var oMenuButton2 = new YAHOO.widget.Button("menubutton2", { |
6 | type: "menu", |
7 | menu: "menubutton2select" }); |
view plain | print | ? |
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:
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:
1 | var 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.
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:
1 | /* |
2 | Search for an element to place the Menu Button into via the |
3 | Event utilities "onContentReady" method |
4 | */ |
5 | |
6 | YAHOO.util.Event.onContentReady("menubuttonsfromjavascript", function () { |
7 | |
8 | // Instantiate an Overlay instance |
9 | |
10 | var oOverlay = new YAHOO.widget.Overlay("menubutton5menu", |
11 | { visible: false }); |
12 | |
13 | oOverlay.setBody("Menu Button 5 Menu"); |
14 | |
15 | // Instantiate a Menu Button |
16 | |
17 | var oMenuButton5 = new YAHOO.widget.Button({ type: "menu", |
18 | label: "Menu Button 5", menu: oOverlay }); |
19 | |
20 | /* |
21 | Append the Menu Button and Overlay to the element with the id |
22 | of "menubuttonsfromjavascript" |
23 | */ |
24 | |
25 | oMenuButton5.appendTo(this); |
26 | |
27 | oOverlay.render(this); |
28 | |
29 | }); |
view plain | print | ? |
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.
1 | // "click" event handler for each item in the Button's menu |
2 | |
3 | function onMenuItemClick(p_sType, p_aArgs, p_oItem) { |
4 | |
5 | oMenuButton4.set("label", p_oItem.cfg.getProperty("text")); |
6 | |
7 | } |
8 | |
9 | // Create an array of YAHOO.widget.MenuItem configuration properties |
10 | |
11 | var aMenuButton4Menu = [ |
12 | |
13 | { text: "one", value: 1, onclick: { fn: onMenuItemClick } }, |
14 | { text: "two", value: 2, onclick: { fn: onMenuItemClick } }, |
15 | { text: "three", value: 3, onclick: { fn: onMenuItemClick } } |
16 | |
17 | ]; |
18 | |
19 | /* |
20 | Instantiate a Menu Button using the array of YAHOO.widget.MenuItem |
21 | configuration properties as the value for the "menu" configuration |
22 | attribute. |
23 | */ |
24 | |
25 | var oMenuButton4 = new YAHOO.widget.Button({ |
26 | type: "menu", |
27 | label: "one", |
28 | name: "mymenubutton", |
29 | menu: aMenuButton4Menu, |
30 | container: "menubuttonsfromjavascript" }); |
view plain | print | ? |
INFO1214ms (+1) 11:09:44 AM:Button yui-gen4
Initialization completed.
INFO1213ms (+0) 11:09:44 AM:Button yui-gen4
No source HTML element. Building the button using the set of configuration attributes.
WARN1213ms (+1) 11:09:44 AM:global
No value specified for the button's "id" attribute. Setting button id to "yui-gen4".
INFO1212ms (+1) 11:09:44 AM:Button menubutton3
Initialization completed.
INFO1211ms (+0) 11:09:44 AM:Button menubutton3
Source element could not be used as is. Creating a new HTML element for the button.
INFO1211ms (+0) 11:09:44 AM:Button menubutton3
Setting attribute "value" using source element's attribute value of "Menu Button 3"
INFO1211ms (+0) 11:09:44 AM:Button menubutton3
Setting attribute "name" using source element's attribute value of "menubutton3_button"
INFO1211ms (+0) 11:09:44 AM:Button menubutton3
Building the button using an existing HTML element as a source element.
INFO1211ms (+2) 11:09:44 AM:Button menubutton2
Initialization completed.
INFO1209ms (+0) 11:09:44 AM:Button menubutton2
Source element could not be used as is. Creating a new HTML element for the button.
INFO1209ms (+0) 11:09:44 AM:Button menubutton2
Setting attribute "value" using source element's attribute value of "Menu Button 2"
INFO1209ms (+0) 11:09:44 AM:Button menubutton2
Setting attribute "name" using source element's attribute value of "menubutton2_button"
INFO1209ms (+0) 11:09:44 AM:Button menubutton2
Building the button using an existing HTML element as a source element.
INFO1209ms (+12) 11:09:44 AM:Button menubutton1
Initialization completed.
INFO1197ms (+0) 11:09:44 AM:Button menubutton1
Source element could not be used as is. Creating a new HTML element for the button.
INFO1197ms (+0) 11:09:44 AM:Button menubutton1
Setting attribute "value" using source element's attribute value of "Menu Button 1"
INFO1197ms (+0) 11:09:44 AM:Button menubutton1
Setting attribute "name" using source element's attribute value of "menubutton1_button"
INFO1197ms (+45) 11:09:44 AM:Button menubutton1
Building the button using an existing HTML element as a source element.
INFO1152ms (+2) 11:09:44 AM:Button yui-gen0
Initialization completed.
INFO1150ms (+0) 11:09:44 AM:Button yui-gen0
No source HTML element. Building the button using the set of configuration attributes.
WARN1150ms (+1150) 11:09:44 AM:global
No value specified for the button's "id" attribute. Setting button id to "yui-gen0".
INFO0ms (+0) 11:09:43 AM:global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings