This example demonstrates different ways to create and use a Split Button.
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:
<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 Split 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 Split Button, pass the id of the source element as the first argument to the Button's constructor. Set the "type" configuration attribute to "split" 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.
The most common events to listen for on a Split Button are click
events. A standard click
event is fired when the face/text label
of a Split Button is clicked. Use the on
method or
onclick
configuration attribute to listen for the click
event.
(Note: The click
event is not fired when the user clicks
on the area of the Button that displays the Button's Menu.)
Split Buttons also fire an
option
event when the user clicks on the area the that invokes the display of its menu.
Use the on
method to listen for the
option
event.
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 Split Button from markup is to begin with an <input>
element and the markup format required for Overlay:
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 "split" 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 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 Overlay completely from JavaScript. Begin by instantiating an Overlay. Then instantiate a Split Button, setting its "type" configuration attribute to "split" 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 Split 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: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings