|
||
| Inside Technique : Lookup Lists This article combines a standard text box and list box to create a lookup list. As the user types into the text box, the list item that most closely matches is automatically highlighted. This example runs in both Internet Explorer 4.0 and Netscape Navigator 4.0 without the need for any custom browser code. |
|
Lookup lists are created with a standard textbox and listbox and are associated through their events. The two events that are tracked are the onchange event on the listbox and the onkeyup event on the textbox. Every time the user selects for the listbox, the selected item's text is updated in the textbox, and conversely each character the user types in the textbox causes the listbox to update with the nearest match. The following HTML demonstrates how this example was created:
In the SELECT listbox, the onchange event calls the In both the listbox and textbox a CSS width is defined to force both the textbox and listbox to line up. The width property is defined by CSS1 to define the size of the element. Only Internet Explorer 4.0 supports this CSS1 property. In Netscape Navigator you need to approximate the width of the textbox using the SIZE attribute in relation to the size of the SELECT element. The TABLE around the elements is used for layout purposes. While Netscape does not honor the CSS width property, this property has an effect on the spacing of the form elements when specified outside of the table. In the above example, I have chosen to use the VALUE field to specify a destination URL. When you click on the Go button, a new window is opened with the specified URL. You can customize and use the list text and its value based on your needs. The CodeThe code in each of the functions is very simple
and involves no browser detection. When the user selects an item from the listbox,
the text of the listbox is copied to the textbox. This function works
in all Javascript aware browsers:
The function that does the lookup is slightly more involved but actually
would also run in all JavaScript aware browsers. The reason this example is limited
to the 4.0 browsers is the lack of the keyboard events in older browsers. The The Go button contains really simple code that navigates the user to
the URL specified by the selected item's OPTION value:
You can use similar techniques to build your own custom input controls from the built-in controls. In the 4.0 browsers, the input controls now have events for real-time tracking of the user's input. We will explore these events and this technique more in future articles. © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |