|
||
| Inside Technique : Lookup Lists II : Creating a Lookup List Lookup lists are created by combining a standard textbox and listbox and associating them through their events. In the 4.0 browsers, the two events that are tracked are the onchange event on the listbox and the onkeyup event on the textbox. In the 3.0 browsers, the onfocus and onblur events are tracked. In the 3.0 browsers, whenever the textbox receives the focus a timer is started that is used to check the current value of the control. When the user leaves the textbox, the timer is turned back off. Using this timer we are able to simulate key events. The following demonstrates the HTML necessary to create this example:
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 and the 3.0 browsers 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 this example, I used 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. Code ReviewThe 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 and is where we
do quite a bit of browser detection. For the 3.0 browsers, we quickly reset the focus()
to force the input boxes value property to be updated. The body of the function then
matches the current input against the choices. The end of the function then resets
the timer for 3.0 browsers ensuring this function is called again after a half-second.
In 4.0 browsers, rather than use the timer, 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 out of standard HTML controls. Using our timer trick, you can even do real-time checking of the user's input in the 3.0 browsers. The last page provides easy access to all the code necessary to create the lookup list. Page 1:Lookup Lists II © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |