Selectmenu Widgetversion added: 1.11
Description: Duplicates and extends the functionality of a native HTML select element to overcome the limitations of the native control.
Selectmenu transforms a <select>
element into a themeable and customizable control. The widget acts as a proxy to the original <select>
; therefore the original element's state is maintained for form submission and serialization.
Selectmenu supports <optgroup>
elements and custom markup to render specific presentations like multiple lines. The <select>
and its options can be disabled by adding a disabled
attribute.
Note: Support for accesskey
on custom elements is extremely limited in browsers. As such, if there is an accesskey
attribute on the <select>
element, it will not work with the custom selectmenu. If there is an accesskey
attribute on any of the <option>
elements, using the accesskey may cause the original element and the custom element to be out of sync. However, most browsers don't support accesskey
on <option>
elements.
Keyboard interaction
When the menu is open, the following key commands are available:
-
UP
/LEFT
: Move focus to the previous item. -
DOWN
/RIGHT
: Move focus to the next item. -
END
/PAGE DOWN
: Move focus to the last item. -
HOME
/PAGE UP
: Move focus to the first item. -
ESCAPE
: Close the menu. -
ENTER
/SPACE
: Select the currently focused item and close the menu. -
ALT
/OPTION
+UP
/DOWN
: Toggle the visibility of the menu.
When the menu is closed, the following key commands are available:
-
UP
/LEFT
: Select the previous item. -
DOWN
/RIGHT
: Select the next item. -
END
/PAGE DOWN
: Select the last item. -
HOME
/PAGE UP
: Select the first item. -
ALT
/OPTION
+UP
/DOWN
: Toggle the visibility of the menu. -
SPACE
: Open the menu.
Theming
The selectmenu widget uses the jQuery UI CSS framework to style its look and feel. If selectmenu specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes
option:
-
ui-selectmenu-button
: The button-like element replacing the native selectmenu on the page. Has theui-selectmenu-button-closed
class when closed, theui-selectmenu-button-open
class when open.-
ui-selectmenu-text
: The span representing the text portion of the button element. -
ui-selectmenu-icon
: The icon within the selectmenu button.
-
-
ui-selectmenu-menu
: The wrapper element around the menu used to display options to the user (not the menu itself). When the menu is open, theui-selectmenu-open
class is added.-
ui-selectmenu-optgroup
: One of the elements that replicates<optgroup>
elements from native selects.
-
Dependencies
Additional Notes:
- This widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS file as a starting point.
Options
appendTo
null
null
, the parents of the <select>
are checked for a class name of ui-front
. If an element with the ui-front
class name is found, the menu is appended to that element. Regardless of the value, if no element is found, the menu is appended to the body.Initialize the selectmenu with the appendTo
option specified:
1
2
3
|
|
Get or set the appendTo
option, after initialization:
1
2
3
4
5
|
|
classes
|
Specify additional classes to add to the widget's elements. Any of classes specified in the Theming section can be used as keys to override their value. To learn more about this option, check out the learn article about the classes
option.
Initialize the selectmenu with the classes
option specified, changing the theming for the ui-selectmenu-menu
class:
1
2
3
4
5
|
|
Get or set a property of the classes
option, after initialization, here reading and changing the theming for the ui-selectmenu-menu
class:
1
2
3
4
5
|
|
disabled
false
true
.Initialize the selectmenu with the disabled
option specified:
1
2
3
|
|
Get or set the disabled
option, after initialization:
1
2
3
4
5
|
|
icons
{ button: "ui-icon-triangle-1-s" }
- button (string, default: "ui-icon-triangle-1-s")
Initialize the selectmenu with the icons
option specified:
1
2
3
|
|
Get or set the icons
option, after initialization:
1
2
3
4
5
|
|
position
{ my: "left top", at: "left bottom", collision: "none" }
Initialize the selectmenu with the position
option specified:
1
2
3
|
|
Get or set the position
option, after initialization:
1
2
3
4
5
|
|
width
false
null
, the width of the native select is used. When the value is false
, no inline style will be set for the width, allowing the width to be set in a stylesheet.Initialize the selectmenu with the width
option specified:
1
2
3
|
|
Get or set the width
option, after initialization:
1
2
3
4
5
|
|
Methods
close()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the close method:
1
|
|
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
1
|
|
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
1
|
|
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
1
|
|
instance()Returns: Object
Retrieves the selectmenu's instance object. If the element does not have an associated instance, undefined
is returned.
Unlike other widget methods, instance()
is safe to call on any element after the selectmenu plugin has loaded.
- This method does not accept any arguments.
Invoke the instance method:
1
|
|
open()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the open method:
1
|
|
option( optionName )Returns: Object
Gets the value currently associated with the specified optionName
.
Note: For options that have objects as their value, you can get the value of a specific key by using dot notation. For example, "foo.bar"
would get the value of the bar
property on the foo
option.
-
optionNameType: StringThe name of the option to get.
Invoke the method:
1
|
|
option()Returns: PlainObject
- This signature does not accept any arguments.
Invoke the method:
1
|
|
option( optionName, value )Returns: jQuery (plugin only)
Sets the value of the selectmenu option associated with the specified optionName
.
Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName
. For example, "foo.bar"
would update only the bar
property of the foo
option.
-
optionNameType: StringThe name of the option to set.
-
valueType: ObjectA value to set for the option.
Invoke the method:
1
|
|
option( options )Returns: jQuery (plugin only)
-
optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
1
|
|
refresh()Returns: jQuery (plugin only)
<option>
or <optgroup>
elements that were added, removed or disabled.
- This method does not accept any arguments.
Invoke the refresh method:
1
|
|
widget()Returns: jQuery (plugin only)
jQuery
object containing the button element.- This method does not accept any arguments.
Invoke the widget method:
1
|
|
Extension Points
The selectmenu widget is built with the widget factory and can be extended. When extending widgets, you have the ability to override or add to the behavior of existing methods. The following methods are provided as extension points with the same API stability as the plugin methods listed above. For more information on widget extensions, see Extending Widgets with the Widget Factory.
_renderButtonItem( item )Returns: jQuery
Method that controls the creation of the generated button content. The method must create and return a new element.
-
itemType: Object
-
disabledType: BooleanWhether the item is disabled.
-
elementType: jQueryA reference to the item's original
<option>
element. -
indexType: NumberThe numeric index of the item.
-
labelType: StringThe string to display for the item.
-
optgroupType: StringIf the item is within an
<optgroup>
, this is set to that<optgroup>
's label. -
valueType: StringThe
value
attribute of the item's original<option>
.
-
Style the button background color based on the value of the selected option.
1
2
3
4
5
6
7
8
9
10
|
|
_renderItem( ul, item )Returns: jQuery
Method that controls the creation of each option in the widget's menu. The method must create a new <li>
element, append it to the menu, and return it. See the Menu documentation for more details about the markup.
-
ulType: jQueryThe
<ul>
element that the newly created<li>
element must be appended to. -
itemType: Object
-
elementType: jQueryThe original
<option>
element. -
indexType: IntegerThe index of the
<option>
within the<select>
. -
valueType: StringThe value of the
<option>
. -
labelType: StringThe label of the
<option>
. -
optgroupType: StringThe label for the parent
optgroup
, if any. -
disabledType: BooleanWhether the
<option>
is disabled.
-
Style the menu item background colors based on the value of their corresponding option elements.
1
2
3
4
5
6
7
8
9
10
11
12
|
|
_renderMenu( ul, items )Returns: jQuery (plugin only)
<ul>
and an array of items based on the <option>
elements in the original <select>
. Creation of the individual <li>
elements should be delegated to _renderItemData()
, which in turn delegates to the _renderItem()
extension point.
-
ulType: jQueryAn empty
<ul>
element to use as the widget's menu. -
itemsType: ArrayAn array of items based on the
<option>
elements in the original<select>
. See the_renderItem()
extension point for details on the format of the item objects.
Add a CSS class name to the odd menu items.
1
2
3
4
5
6
7
|
|
_resizeMenu()Returns: jQuery (plugin only)
this.menu
.- This method does not accept any arguments.
Always display the menu as 500 pixels wide.
1
2
3
|
|
Events
change( event, ui )Type: selectmenuchange
select
event will fire a change
event.Initialize the selectmenu with the change callback specified:
1
2
3
|
|
Bind an event listener to the selectmenuchange event:
1
|
|
close( event )Type: selectmenuclose
-
eventType: Event
Note: The ui
object is empty but included for consistency with other events.
Initialize the selectmenu with the close callback specified:
1
2
3
|
|
Bind an event listener to the selectmenuclose event:
1
|
|
create( event, ui )Type: selectmenucreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the selectmenu with the create callback specified:
1
2
3
|
|
Bind an event listener to the selectmenucreate event:
1
|
|
focus( event, ui )Type: selectmenufocus
Initialize the selectmenu with the focus callback specified:
1
2
3
|
|
Bind an event listener to the selectmenufocus event:
1
|
|
open( event )Type: selectmenuopen
-
eventType: Event
Note: The ui
object is empty but included for consistency with other events.
Initialize the selectmenu with the open callback specified:
1
2
3
|
|
Bind an event listener to the selectmenuopen event:
1
|
|
select( event, ui )Type: selectmenuselect
Initialize the selectmenu with the select callback specified:
1
2
3
|
|
Bind an event listener to the selectmenuselect event:
1
|
|
Examples:
A simple jQuery UI Selectmenu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
|
Demo:
A simple jQuery UI Selectmenu with optgroups
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
|
Demo:
A jQuery UI Selectmenu with overflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
|