The jQuery dropdownReplacement plugin allows you to and unobtrusively upgrade inputs and native selects into a dropdown widget that is indistingushable from a native select box in both look and functionality.
Furthermore, this plugin provides numerous options for customization, and has been tested on all major browsers and is being used in a production enviorment.
Quick Start Guide
Converting an existing select
Find an existing select box to convert:
Include the scripts and styles required by the dropdownReplacement plugin and a short script to initialize the dropdown when the DOM is ready:
This will yeild a widgetized select:
<- Yes that is a javascript widget you are looking at.
Widgetizing an input
Its not always optimal to build a widget from an existing select, for that reason you have an option to widgetize an input box by providing a list of options to use in JSON format.
You start out with an input that can contain the default text selection of the to-be dropdown:
Include the scripts and styles required by the dropdownReplacement plugin and a short script to initialize the dropdown when the DOM is ready:
This will yeild a widgetized select:
Those are the basics! For more complex demos see the examples tab or take a look at the API.
The simplest way to upgrade this to a dropdown widget is:
Note how the dropdown extends over the width of the select box. This is usually desired. If you select text that does not fit into the select box, it gets an ellipsis added to it automatically.
You can use up and down arrows to navigate the select box, also you can type letters in order to jump to that letter.
Control the number of items shown:
Dropdown is resized to fit the options:
Dropdown is resized to 100px, and a change function is wired:
Widgetizing an input
You have more control when you widgetize an input, or a set of inputs.
Five inputs are widgetized to use the same dropdown body. This is very useful in reports where you have to make identical selections for sets of items. This saves bandwidth as well.
The last param in the change function gives you the index of the triggering input.
You can widgetize any number of inputs. You can only widgetize one select at a time.
A single input is widgetized, but two dropdown widgets are wired into it. This means that both dropdowns feed into one form input. This is useful when you want to show two different inputs, usually one at a time, but simplify the serverside by using a single form name.
You can wire any number of dropdowns into a single input, just make sure you hide some of them because they will all be visible when created.
Change dropdown:
Using Advanced Options
Resize options to fit (a large) select:
There is another option resizeOptionsToFitSelect which defaults to true. Usually you want to resize the options, but not the select.
A dictionary tree of the dropdown options is built to do quick selections when user types a letter or letters. By default the dictionary is 3 letters deep, meaning that only the first 3 letters of each option are added to the dictionary.
The option lookupMaxWordLength sets the number of letters to remember. Increasing this number will lead to slower widget init time. The question of "how much slower?" is up to you to answer.
Go ahead and type some weird european city names into the dropdown and watch it seek to them. Press 'b' a few times in a row and see what happens.
In order to make the dropdown widget less of a CPU hog, keyboard events are debounced, this can make the widget seem less responsive. You can use the options
debounceLookupMs and debounceArrowsMs to change the debounce intervals.
The default values are debounceLookupMs=200 and debounceArrowsMs=50. This dropdown should seem more responsive than the others (unless it hogs your CPU).
There are 3 options which control the ellipsis functionality. If ellipsisSelectText ellipsisText by default is set to "..." but can be anything.
Normally charWidth is null, which means that determining where to chop of the text is done using magic. If the magic fails, you can set the character width that is used in the calculation here. Double values work fine.
Not surprisingly, the text is chopped off much sooner then it needs to be now.