Saturday, October 13, 2007

HTML ListBox Quick Item Select JavaScript

It has been awhile given that I have been busy at work and also started a graduate business informatics program. Anywho, a customer recently requested a way to select an HTML Listbox control and key in an account number and have that number be selected as they were entering the perspective number.

A coworker and I came up with the following. The referenced JavaScript file is here.

The trick here was utilizing the onkeyup event. The entered text is appended to a string that is compared to the listbox content. If there is a match, it is added to an array of matched items. On each onkeyup event, the first matched element in the array is selected in the listbox control. If the listbox loses focus, then entered string is set to an empty string.

No comments: