YUI Library Home

YUI Library Examples: Button Control: Split Buttons

Button Control: Split Buttons

This example demonstrates different ways to create and use 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.

Creating Split Buttons Using Existing Markup

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

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.

Adding Click Event Listeners To A Split Button

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.

Creating Split Buttons Using JavaScript

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.

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 © 2011 Yahoo! Inc. All rights reserved.

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