This page demonstrates how a simple INPUT type=text HTML Element can be made to implement the functionality of a ComboBox Control by assigning a function to its onclick or onfocus event that will manipulate its behavior while in its edit mode. This provides a better alternative when a more flexible user interaction is required for which, the SELECT Element, with its restrictive and limited functionality, may not be able to provide.
The examples on this page emulate a ComboBox Control with the following behaviors which can be configured and modified by providing and setting additional attributes to the INPUT Element :
The underlying script runs fairly well with IE6 and FireFox 2.0. (The author is completely ignorant in scripting for other browsers, has no time yet to learn and in fact has been struggling quite a bit for making it run with FireFox.) You may notice slightly different behaviors between the two and the most notable is when text is made to auto-expand. I noticed an annoying blinking effect on the selected portion everytime a character is typed-in with FireFox. The text selection goes smoothly with IE. It's most probably due to the coding done.
Try the examples here having different settings. You'd note that we don't have a special object being added. Just two DIV Elements, one containing the listbox which inner contents are dynamically created and modified, and another which serves as the listbox background which is useful when you want to make use of some filter and create a semi-transparent effect. And yes of course, we have an IMG Element for the button. This means that it's not different ComboBox Control objects for each example but rather just the same function made to run for each INPUT Element.
1) ComboBox Control with its default settings: Text auto-expand/complete off, value matching done on any part of the field, input not limited to list and text value not found on list is not added, and nearest or matched item on list is not highlighted.
Your country
2) ComboBox Control with the following turned on: Text auto-expand/complete, limit to list and highlight matched item on list. Matching on any part of field is turned off.
This list contains more than 7,000 items.A list as big or even bigger than above will take time to render its HTML contents. Typing the character 'e' above as the first value matches about 7161 items (matching on any part of field turned on) and when the textbox is blank, returns all the choices. The match is done relatively fast but to render everthing might be noticeably slow. To overcome such, there is a portion of the script to limit the items to be rendered. In this example, it's set to render a maximum of 300 choices in the listbox.
3) This one demonstrates how matching on any part of the field is useful. Type either value of each column and a match (if it exists of course) can still be found.
Days of the weekIn actual application, you may need additional scripting if only the value of a particular column is needed.
4) ComboBox Control with an initial empty list and the add to list property set to true. Build the list with new values.
Type anything
5) This one just wants to find out what you really are (out of curiosity).
Sex
| Attribute | Default value |
|---|---|
| cbAutoExpand | false |
| cbLimitToList | false |
| cbMatchOnAnyPart | true |
| cbHighLightMatch | false |
| cbAddToList | false |
The location coordinates for the listbox in the script may not work properly when your INPUT Element is contained in several layers of parent elements having different dimensions and scrollable values and having relative positioning properties. You may need to modify the script to set the correct location. The script doesn't also take into account whether the listbox needs to be located below or above the textbox depending on the window area visible.
For other things like the height or the default maximum items in the listbox, font type and sizes, and other user events you may wish to consider and when text values need to update underlying recordsets, etc.,etc., etc., you can make customized changes and add additional global variables or element attributes. A column heading can also be easily added to the listbox.
Yes, you can. Assign this handler to the BODY onload event:
And this one was really a SELECT Element until the document has loaded.
Profession
The script also includes jsfilter() which is a javascript/jscript attempt of the vbscript FILTER function which utilizes regular expression. RegExp is also used to get the choice index and search through the list for a match value instead of iterating through each choice which would be evidently slow for a list containing thousands of entries.