Yahoo! Developer Network Home - Help

YUI Library Examples: Button Control: Split Buttons

Button Control: Split Buttons

This example demonstrates different ways to create a Split Button.

Split Buttons
From Markup
Split Button 3 Menu
From JavaScript

Creating Split Buttons

With the inclusion of the optional Menu library, it is possible to create Buttons that incorporate a menu.

Split Buttons can be created with or without existing HTML. In either case, create a Split Button by setting the "type" configuration attribute to "split" 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 Split 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="splitbutton1" name="splitbutton1_button" value="Split Button 1"
2<select id="splitbutton1select" name="splitbutton1select" multiple> 
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="splitbutton2" name="splitbutton2_button" value="Split Button 2"
9<select id="splitbutton2select" name="splitbutton2select"
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 Split 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 oSplitButton1 = new YAHOO.widget.Button("splitbutton1", { type: "split"
2                                        menu: "splitbutton1select" }); 
3 
4var oSplitButton2 = new YAHOO.widget.Button("splitbutton2", { type: "split",  
5                                        menu: "splitbutton2select" }); 
view plain | print | ?

Please note: If the source <input/> element's type was "submit," the Split 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 Split Button from markup is to begin with an <input/> element and the markup format required for YAHOO.widget.Overlay:

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

To instantiate the Split 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 oSplitButton3 = new YAHOO.widget.Button("splitbutton3", { type: "split",  
2                                        menu: "splitbutton3menu" }); 
view plain | print | ?

Using an Overlay instance as a Split 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 Split Button that utilizes YAHOO.widget.Overlay completely from JavaScript. Simply instantiate and render an Overlay instance. Then instantiate a Split 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 Split Button into via the 
3    Event utilities "onContentReady" method
4*/ 
5 
6YAHOO.util.Event.onContentReady("splitbuttonsfromjavascript"function () { 
7 
8    // Instantiate an Overlay instance 
9 
10    var oOverlay = new YAHOO.widget.Overlay("splitbutton5menu",  
11                                        { visible: false }); 
12     
13    oOverlay.setBody("Split Button 5 Menu"); 
14 
15 
16    // Instantiate a Split Button 
17 
18    var oSplitButton5 = new YAHOO.widget.Button({ type: "split",   
19                                label: "Split Button 5", menu: oOverlay }); 
20 
21    /*
22         Append the Split Button and Overlay to the element with the id 
23         of "splitbuttonsfromjavascript"
24    */ 
25 
26    oSplitButton5.appendTo(this); 
27 
28    oOverlay.render(this); 
29                     
30}); 
view plain | print | ?

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

1//  Create an array of YAHOO.widget.MenuItem configuration properties 
2 
3var aSplitButton4Menu = [ 
4 
5    { text: "one", value: 1 }, 
6    { text: "two", value: 2 }, 
7    { text: "three", value: 3 } 
8 
9]; 
10 
11/*
12    Instantiate a Split Button using the array of YAHOO.widget.MenuItem 
13    configuration properties as the value for the "menu" configuration 
14    attribute.
15*/ 
16 
17var oSplitButton4 = new YAHOO.widget.Button({ type: "split",   
18                                    label: "Split Button 4",  
19                                    name: "splitbutton3",  
20                                    menu: aSplitButton4Menu,  
21                                    container: "splitbuttonsfromjavascript" });      
view plain | print | ?

Copyright © 2008 Yahoo! Inc. All rights reserved.

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