Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlight typed text in drop down? 4

Status
Not open for further replies.

RichS

Programmer
Apr 24, 2000
380
US
I want to provide a drop down box that will highlight, in the text box area, the first item in the list that matches the typed text. For example, if the items in a list are China and Chicago and the user types in "chi", "Chicago" appears in the text portion of the dropdown then if the next letter the user types is an "n" then "China" is displayed in the text box portion of the dropdown.

I don't even know what this functionality is called so any help in doing this would be greatly appreciated.

Rich

 
<html>

<head>
<title>comboBox</title>
<script language=&quot;JavaScript&quot;>
function autoComplete (field, select, property) {
var found = false;
for (var i=0;i<select.options.length;i++) {
if(select.options[property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
found=true; break;
}
}
if (found) { select.selectedIndex = i; }
else { select.selectedIndex = -1; }
if (field.createTextRange) {
if (!found) {
field.value=field.value.substring(0,field.value.length-1);
return;
}
var cursorKeys =&quot;8;46;37;38;39;40;33;34;35;36;45;&quot;;
if (cursorKeys.indexOf(event.keyCode+&quot;;&quot;) == -1) {
var r1 = field.createTextRange();
var oldValue = r1.text;
var newValue = found ? select.options[property] : oldValue;
if (newValue != field.value) {
field.value = newValue;
var rNew = field.createTextRange();
rNew.moveStart('character', oldValue.length) ;
rNew.select();
}
}
}
}</script>
</head>

<body>

<form>
<input ONKEYUP=&quot;autoComplete(this,this.form('test'),'text')&quot;>
<select name=&quot;test&quot;>
<option value=&quot;1&quot;>one
<option value=&quot;2&quot;>two
<option value=&quot;3&quot;>three
</select>

Adam
 
Thanks Adam,
Do you know of a way to do it all within the same element without the additional <input type=text> box?

Rich
 
I made this script that will insert a textbox and position it on top of the select box. It then clips the right side of the textbox and the left side of the select box so the down arrow looks like part of the text box. It works pretty well for Windows 2000 on IE. I've never used this in real applications before because I'm guessing it would look pretty bad on Windows XP or Macs (since their textboxes are different). But maybe it'll help you.



<html>

<head>
<title>comboBox</title>
<script language=&quot;JavaScript&quot;>
// New Event! Add onNotInList=&quot;&quot; to your select box to fire an event when the typed value is not an option of the drop-down.

var boxWidth = 200; // Width of the select boxes
var comboAll = false; // true = all select boxes; false = only select boxes with the attribute combo=&quot;combo&quot;
var forceMatch = true; // true = typed value must exist in the drop-down on blur; false = any value accepted on blur.
var forceAutoMatch = false; // true = only allows values in the drop-down to be typed; false = any value accepted.


var f;
function initCbo(){
var output='';
for(var f=0;f<document.forms.length;f++){
var s=document.forms[f].elements;
for(var i=0;i<s.length;i++){
if(s.type == 'select-one' && (s.combo=='combo' || comboAll==true)){
output='';
s.style.position='absolute';
s.style.width='100%';
s.style.zIndex='1';
s.style.clip='rect(auto,auto,auto,'+(boxWidth-20)+')';
s.onfocus=&quot;window.clearTimeout(window.tmr&quot;+s.name+&quot;)&quot;;
s.onchange='setTextBox(this,this.form)';
output+='<input type=&quot;text&quot; name=&quot;'+s.name+'Box&quot; size=&quot;20&quot; style=&quot;position: absolute; width: '+(boxWidth-18)+'; z-index: 2;clip:rect(auto,'+(boxWidth-19)+',auto,auto)&quot; onblur=&quot;checkInList(\''+s.name+'\',this.form)&quot; ONKEYUP=&quot;autoComplete(this,this.form(\''+s.name+'\'),\'text\')&quot; onNotInList=&quot;'+s.onNotInList+'&quot;>';
output='<span style=&quot;position:relative;width:'+boxWidth+'px;height:22px&quot;>'+s.outerHTML+output+'</span>';
s.outerHTML=output;
}
}
}
}

function checkInList(listName,f){
if(forceMatch){
var found=false;
var box=f[listName+&quot;Box&quot;];
var list=f[listName];
for(var i=0;i<list.length;i++){
if(box.value==list.text || box.value==''){
found=true;
break;
}
}
if(!found){
window.f=f;
eval(&quot;window.tmr&quot;+listName+&quot;=setTimeout(\&quot;notInList('&quot;+box.name+&quot;')\&quot;,100)&quot;);
}
}
}
function notInList(box){
alert('The value you typed could not be found in the list!');
eval(f[box].onNotInList);
f[box].select();
f[box].focus();
}
function setTextBox(list,f){
var box = f[list.name+&quot;Box&quot;]
box.value=list[list.selectedIndex].text;
box.select();
box.focus();
}
function autoComplete (field, select, property) {
var found = false;
for (var i=0;i<select.options.length;i++) {
if(select.options[property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
found=true; break;
}
}
if (found) { select.selectedIndex = i; }
else { select.selectedIndex = -1; }
if (field.createTextRange) {
if (forceAutoMatch && !found) {
field.value=field.value.substring(0,field.value.length-1);
return;
}
var cursorKeys =&quot;8;46;37;38;39;40;33;34;35;36;45;&quot;;
if (cursorKeys.indexOf(event.keyCode+&quot;;&quot;) == -1) {
var r1 = field.createTextRange();
var oldValue = r1.text;
var newValue = found ? select.options[property] : oldValue;
if (newValue != field.value) {
field.value = newValue;
var rNew = field.createTextRange();
rNew.moveStart('character', oldValue.length) ;
rNew.select();
}
}
}
}
window.onload=initCbo; // initalizes the script.
</script>
</head>

<body>

<form>
<table border=1>
<tr><td>label1</td>
<td><select name=&quot;test&quot; combo=&quot;combo&quot;>
<option value=&quot;1&quot;>one
<option value=&quot;2&quot;>two
</select></td></tr>
<tr><td>label2</td>
<td><select name=&quot;test2&quot;>
<option value=&quot;1&quot;>one
<option value=&quot;2&quot;>two
</select></td></tr></table>
</form>

</body>

</html>


Adam
 
Wow... This works great on W2000 with IE 6.0 which is the standard we use. You are my new JS role model. ;) I've been trying for a while to get this to work. Now I just gotta go see how you did it.

Thanks a bunch!

Rich
 
Your textbox-over-select solution does look pretty good in IE6 & Win2K. Nice one :)

RichS, I think a lot of users when presented with a big select (ie, it has a scrollbar) will type in the first letter of the word they want to jump to. Since its built in, you don't have to do anything..

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
True, but I am developing an application for use by blind people. If the user is searching for last name &quot;Mazander&quot; they may have to press the &quot;M&quot; character 200 times to get to the record that they want (such as in a phone book). Blind people only use the keyboard and each item in the list must be read to them using a screen reader. Being able to search by typing will be a big time saver.

Thanks for the feedback.

Rich
 
This is a good script I have been looking. Thanks, any side effects I should work on before using it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top