| AutoSearch SELECT tag |
| Written by Oscar Bowyer |
| Friday, 30 January 2009 16:00 |
IntroductionIf you spend any time surfing the web you will find <select> lists. In all cases I"ve encountered the default single character searches are used to help you "quickly" find the list item you want. As an example, to reach North Carolina in a list of states you would type "N" which takes you to Nebraska. "O" will take you to Ohio, so you will quickly learn to type "N" and down arrow 6 times to get "North Carolina". I think we can do better. An auto searching <select> list would allow us to type "No" and automatically get "North Carolina". Mississippi and Arkansas are other good examples. A few lines of client side JavaScript can solve this nuisance. I"ve included examples of both types of <select> lists for USA states below so give them a try. Standard Non Searching Select ListAutoSearching Select ListHow it works
Basically the javascript code driving the AutoSearching <select> above records each keystroke in a hidden input tag and uses ALL keystrokes to search for a matching Option in the list. A few questions may come to mind, such as "How to you clear previously captured keystrokes?". The How to use AutoSearch <select>
First, add
Next, add Last, add a hidden input tag named "keys" to store the previous keystrokes A little debugging in case you are an occasional fumble finger typist like me and your <select> tags will never be the same. The Code
// Java Script to Handle AutoSearch
function selectKeyDown()
{
// Delete Key resets previous search keys
if(window.event.keyCode == 46)
clr();
}
function selectKeyPress()
{
// Notes:
// 1) previous keys are cleared onBlur/onFocus and with Delete key
// 2) if the search doesn"t find a match, this returns to normal 1 key
// search setting returnValue = false below for ALL cases will
// prevent default behavior
//TODO:
// 1) add Netscape handling
var sndr = window.event.srcElement;
var pre = this.document.all["keys"].value;
var key = window.event.keyCode;
var char = String.fromCharCode(key);
// "i" -> ignoreCase
var re = new RegExp("^" + pre + char, "i");
for(var i=0; i<sndr.options.length; i++)
{
if(re.test(sndr.options[i].text))
{
sndr.options[i].selected=true;
document.all["keys"].value += char;
window.event.returnValue = false;
break;
}
}
}
function clr()
{
document.all["keys"].value = "";
}
Tested Browsers
TODO
HistoryRelease 03/02/2003 Something wrong with this article? Report it
Set as favorite
Bookmark
Email This
Hits: 111 0 Comments
Write comment
You must be logged in to a comment. Please register if you do not have an account yet.
|
| Last Updated on Friday, 06 February 2009 16:00 |
Latest Articles
- AntiHisto
- Thumbnail images in PHP
- Online Dating Websites
- Hack to enforce the cache of an XmlDataSource to invalidate
- AutoSearch SELECT tag
- Nine ASP.NET Site Navigation Problem Solutions: Part 1
- Enhanced rich edit control
- CheckListBox based on ListBox that supports ReadOnly
- Introduction to COM - What It Is and How to Use It.
- Introduction to the Validation Application Block


