PDA

View Full Version : List Box - how to position an item in the list box based on initial letters typed


BubbleStar
09-16-2002, 08:34 AM
Does anyone know how to allow user to type the first few characters to locate an item in the list box?

for example typing "Au" while selecting the list box with names of countries will bring users directly to "Australia"

Thanx! :confused:

glenngv
09-16-2002, 09:31 AM
you need another field (textbox) where user types in the first few letters of the items in the listbox

http://javascript.internet.com/forms/list-chooser.html

BubbleStar
09-17-2002, 02:33 AM
Thanx for the help..
but what i really need is to narrow down the selection from a combo box.
It is more practical as i'll be implementing it in a form...

Would really appreciate it if anyone could help

thanx!
:o

adios
09-17-2002, 02:39 AM
Did you know that <select>s already do this, when they have keyboard focus? Saves a lot of coding. ;)

BubbleStar
09-17-2002, 02:42 AM
the <select> only narrow down to the first letter..

any other suggestion?

thanx.

adios
09-17-2002, 02:44 AM
What platform are you using? All the ones I've got here accept multiple keystrokes...'austria' changes to 'australia', e.g., as you continue to type it in.

BubbleStar
09-17-2002, 03:48 AM
i'm developing a web application using Windows 2000 Server (OS).

can u show me what u have? it might be of help..

thanx!

adios
09-17-2002, 05:45 AM
Not much! Just setting focus on the drop-down, and hitting keys in sequence. Works for me...

Here's something I was fooling with a while back; not nearly finished (or debugged) and not sure it's worth the effort but, might give you some ideas if the above is unsuccessful:


<html>
<head>
<title>untitled</title>
<style type="text/css">

input {
font: 200 11px "comic sans ms";
color: white;
border: 2px white inset;
background: black;
}

select {
font: 200 11px "comic sans ms";
color: white;
background: black;
}

</style>
<script type="text/javascript" language="javascript">

function setSelect(e, field, s) {
if (typeof setSelect.fieldLength == 'undefined') setSelect.fieldLength = field.value.length;
if (field.value.length<setSelect.fieldLength) {
setSelect.fieldLength = field.value.length;
return;
}
var val = field.value.toLowerCase();
var opt = 1, firstone, howmany = 0;
while (s.options[opt] && s.options[opt].value.substring(0,val.length) != val) ++opt;
firstone = opt;
while (s.options[opt] && s.options[opt++].value.substring(0,val.length) == val) ++howmany;
if (!howmany) {
alert('That entry does not exist.');
field.select();
return false;
}
s.size = 1;
s.selectedIndex = firstone;
s.size = howmany;
if (howmany == 1 && confirm('Correct country?')) {
field.value = s.options[s.selectedIndex].value.toUpperCase();
setSelect.fieldLength = field.value.length;
}
}

</script>
</head>
<body bgcolor="black" onload="document.forms[0].country.select()">
<div style="border-top:3px red double;border-bottom:3px red double;text-align:center;margin-top:10%;">
<form style="margin:0px;">
<input name="country" type="text" value="Enter Country"
onkeyup="if(this.value)setSelect(event,this,countries)">
<select name="countries" size="1">
<option value="">COUNTRY</option>
<option value="albania">Albania</option>
<option value="australia">Australia</option>
<option value="austria">Austria</option>
<option value="cambodia">Cambodia</option>
<option value="cameroon">Cameroon</option>
<option value="canada">Canada</option>
<option value="china">China</option>
<option value="england">England</option>
<option value="eritrea">Eritrea</option>
<option value="france">France</option>
<option value="gambia">Gambia</option>
<option value="germany">Germany</option>
<option value="greece">Greece</option>
<option value="namibia">Namibia</option>
<option value="nicaragua">Nicaragua</option>
<option value="nigeria">Nigeria</option>
<option value="peru">Peru</option>
<option value="philippines">Philippines</option>
<option value="poland">Poland</option>
<option value="vietnam">Vietnam</option>
<option value="united states">United States</option>
<option value="united arab emirates">United Arab Emirates</option>
<option value="uruguay">Uruguay</option>
</select>
</form>
</div>
</body>
</html>


Like I said, just an idea....:o

BubbleStar
09-17-2002, 07:15 AM
thanx adios!

but unfortunately, it isn't exactly wat i want. the concept is there but i would want to use just combo box only(drop down list) instead of having another text field...

do u or anyone out there haf any idea on how i can do this??

:confused:


thanx!

glenngv
09-17-2002, 09:09 AM
try this:


<html>
<head>
<script language="javascript">

var typed="";
var obj; //object handle to the select tag;

function select1(objSelect,e){
var code=(document.all) ? event.keyCode:e.which;
if (code==27){ //ESC key
typed="";
objSelect.selectedIndex=0;
return;
}
var letter = String.fromCharCode(code);
typed+=letter;
obj=objSelect;
setTimeout("select2()",100);//put some delay because select's behavior overrides select2() function.
}

function select2(){
for (var i=0;i<obj.options.length;i++){
if (obj.options[i].text.toLowerCase().indexOf(typed.toLowerCase())==0){
obj.selectedIndex=i;
return;
}
}
obj.selectedIndex=0;
}

</script>
</head>
<body>
<form>
<select name="s" onkeydown="select1(this,e)">
<option>Albania</option>
<option>Australia</option>
<option>Austria</option>
<option>Cambodia</option>
<option>Cameroon</option>
<option>Canada</option>
<option>China</option>
<option>England</option>
<option>Eritrea</option>
<option>Uruguay</option>
</select>
</form>
</body>
</html>


I put a small delay in selecting the option because it happens first before the actual behavior of select tags occur. I've only tested it in IE (and it works!) not in NS6.

BubbleStar
09-17-2002, 09:21 AM
thanx..
i've tried to try out the codes but there was an error message that displays "e is undefined" at line 35 of the codes.

how do i correct this? what is the problem?

glenngv
09-17-2002, 09:31 AM
sorry typo error

change the onkeydown to this:

<select name="s" onkeydown="select1(this,event)">

copy the whole code again, I added some lines:


<html>
<head>
<script language="javascript">

var typed="";
var obj; //object handle to the select tag;

function select1(objSelect,e){
var code=(document.all) ? event.keyCode:e.which;
if (code==27){ //ESC key
typed="";
window.status="";
objSelect.selectedIndex=0;
return;
}
var letter = String.fromCharCode(code);
typed+=letter;
window.status=typed;
obj=objSelect;
setTimeout("select2()",100);//put some delay because select's behavior overrides select2() function.
}

function select2(){
for (var i=0;i<obj.options.length;i++){
if (obj.options[i].text.toLowerCase().indexOf(typed.toLowerCase())==0){
obj.selectedIndex=i;
return;
}
}
obj.selectedIndex=0;
}

</script>
</head>
<body>
<form>
<select name="s" onkeydown="select1(this,event)">
<option>Albania</option>
<option>Australia</option>
<option>Austria</option>
<option>Cambodia</option>
<option>Cameroon</option>
<option>Canada</option>
<option>China</option>
<option>England</option>
<option>Eritrea</option>
<option>Uruguay</option>
</select>
</form>
</body>
</html>



just additional info:

you have to click ESC key to reset what the user typed. it will also reset the selected option to the first item. also if no match is found, it will reset the selected option to the first item.
as you typed inside the select tag, look at the window status bar to see what you are typing.

BubbleStar
09-17-2002, 09:59 AM
thanx

i've tried it out. it work but it can seems rather messy as u can actually see the selection 'jumping' up and down..
And you have to actually press the esc key to reset it.

Hope if there is anyone who knows how to enhance this, pls reply.



thanx so much!
:)

glenngv
09-17-2002, 10:15 AM
that is the CLOSEST that you can get if you really dont want to use another field where user would type the letters. you CANNOT really override the select tag's behavior of auto-selecting the option as you type the first letter of the item.

there should be a way to reset what the user typed like pressing the ESC key. if not, the string could take forever and you won't find a match in the options.

glenngv
09-17-2002, 10:27 AM
Originally posted by glenngv
that is the CLOSEST that you can get if you really dont want to use another field where user would type the letters. you CANNOT really override the select tag's behavior of auto-selecting the option as you type the first letter of the item.

there should be a way to reset what the user typed like pressing the ESC key. if not, the string could take forever and you won't find a match in the options.


FYI, I didn't think that was possible at all until I tried and made that only earlier. :)

BubbleStar
09-18-2002, 04:54 AM
Thanx.. :thumbsup:

but after selecting my chosen item in the combo box, i have to manually use the mouse cursor to move to the next item in the form.
how to i correct this so that i can just move to the next field in the form by pressing the tab key?
And also, after i press the tab key or enter, the combo box value is selected back to the default. this is not practical.

Another problem i faced is that, the combo box need to be "close"
for the selection codes to work. that means, if i pull down the combo box to see the list of countries, the codes won't work anymore..

anyone with any suggestions/solution to these problems??


thanx alot!

glenngv
09-18-2002, 05:30 AM
for the TAB key issue, add an "else if" condition like the snippet below:

//
if (code==27){//ESC key
typed="";
window.status='';
objSelect.selectedIndex=0;
return;
}
else if (code==9){//TAB key
typed="";
window.status='';
return;
}
//


for the "close" combobox issue, give me time to fix it.

glenngv
09-18-2002, 07:47 AM
ok, now ive fixed it! and complete will all your requirements :)



<html>
<head>
<title>Auto-selecting options based on continuous keystrokes</title>
<script language="javascript">

/*
Created by:
Glenn G. Vergara
glenngv@yahoo.com
Philippines
*/

var typed="";
function s1(objSelect,e){
var code=(document.all)?event.keyCode:e.which;
if (code==27){//ESC key
typed="";
window.status='';
objSelect.selectedIndex=0;
return false;
}
else if (code==9){//TAB key
typed="";
window.status='';
return true;
}
var letter = String.fromCharCode(code);
typed+=letter;
window.status=typed;
if (document.all){
for (var i=0;i<objSelect.options.length;i++){
//override SELECT behavior of auto-selecting option on press of the first letter of the item
if (objSelect.options[i].text.substring(0,1).toUpperCase()==letter)
return s2(objSelect); //return false;
}
}
if (document.all) {
//for IE
return s2(objSelect);
}
else{
//for NS6 and up (put a little delay to supercede the SELECT behavior of auto-selecting option on press of the first letter of the item)
setTimeout("s2(document.forms['"+objSelect.form.name+"'].elements['"+objSelect.name+"'])",1);
}
}

function s2(objSelect){
for (var i=0;i<objSelect.options.length;i++){
if (objSelect.options[i].text.toUpperCase().indexOf(typed)==0){
objSelect.selectedIndex=i;
return false;
}
}
objSelect.selectedIndex=0;
return false; //always return false
}

</script>
</head>
<body>
<form name="f">
<h3>Auto-selecting options based on continuous keystrokes</h3>
<select name="s" onkeydown="return s1(this,event)">
<option>Albania</option>
<option>Australia</option>
<option>Austria</option>
<option>Cambodia</option>
<option>Cameroon</option>
<option>Canada</option>
<option>China</option>
<option>England</option>
<option>Holland</option>
<option>Guatemala</option>
<option>Malaysia</option>
<option>Nigeria</option>
<option>Philippines</option>
<option>Uruguay</option>
</select>
<p>
<small>
<b>Note:</b><br>
- Press ESC key to reset keystrokes<br>
- Look at status bar to see what you are typing<br>
- Works with IE and NS6
</small>
</form>
</body>
</html>



Ive tested it in IE 5.5 and NS6 and worked OK.
In IE, there's no more "jumping" up and down of items. I've overriden the auto-select of first letter of the option! :D
In NS6, there's a little "jumping" but quite unnoticeable.

Hope this works for you. Didn't realize this was possible, so if this works for you, allow me to put that credit in the code :)


modified to make it really generic. see posts below for details.

BubbleStar
09-19-2002, 03:06 AM
thanx.. that was really helpful.. :thumbsup:

i'm trying to modify the logic of my form whereby the javascript codes can actually be reused on several other combo boxes in the same form..
i realised that the line in bold below:-


if (document.all){
for (var i=0;i<objSelect.options.length;i++){
//override SELECT behavior of auto-selecting option on press of the first letter of the item
if (objSelect.options[i].text.substring(0,1).toUpperCase()==letter)
return s2(objSelect); //return false;
}
}
if (document.all) {
//for IE
return s2(objSelect);
}
else{
//for IE6 and up (put a little delay to supercede the SELECT behavior of auto-selecting option on press of the first letter of the item)
}setTimeout("s2(document.f.s)",1);
}




(the line in bold) it only calls a specified combo box on form f and combo box name s
How do i change so that other combo boxes (be it on the same form or another) is able to use the function?

glenngv
09-19-2002, 03:26 AM
change it to this to make it generic and you won't modify each page anymore (you can reuse it in other pages as well, without modifying anything on the function)

else{
//for NS6 and up (put a little delay to supercede the SELECT behavior of auto-selecting option on press of the first letter of the item)

setTimeout("s2(document.forms['"+objSelect.form.name+"'].elements['"+objSelect.name+"'])",1);

}

also change the comment IE6 to NS6, it was an honest mistake on my part. The else part should be for NS6 and other browsers that support onkeydown event on SELECT tag, for that matter.

glenngv
09-19-2002, 04:43 AM
here's the latest code again.
i've fixed a bug and enhanced some features.

Bug:
In IE if you type keys as the SELECT tag is "pulled-down" then press the TAB key the selected option is reset to the first item.

Enhancements:
-Removed the resetting of option to the first item if no match is found, instead the selected index is not changed from the previously matched item.
-Added pressing BACKSPACE key to delete last key pressed. Of course, as you press this key, the item matching the new keys will still be selected automatically
-Removed resetting to first option when ESC key is pressed but instead resets to previous selection and clears the typed keys.
-Removed unnecessary statements.


here's the whole code:




<html>
<head>
<title>Auto-selecting options based on continuous keystrokes</title>
<script language="javascript">

/*
Created by:
Glenn G. Vergara
glenngv@yahoo.com
Philippines
*/

var typed="";
var index=0;
var prev=0;
function s1(objSelect,e){
var letter="";
var code=(document.all)?event.keyCode:e.which;
if (code==27){ //ESC key
typed="";
window.status='';
index=prev;
objSelect.selectedIndex=index;
return true;
}
else if (code==9){ //TAB key
typed="";
window.status='';
return true;
}
else if (code==8){ //BACKSPACE key
typed=typed.substring(0,typed.length-1);
}
else letter = String.fromCharCode(code);

typed+=letter;
window.status=typed;
if (document.all) {
//for IE
return s2(objSelect);
}
else{
//for NS6 and up (put a little delay to supercede the SELECT behavior of auto-selecting option on press of the first letter of the item)
setTimeout("s2(document.forms['"+objSelect.form.name+"'].elements['"+objSelect.name+"'])",1);
}
}

function s2(objSelect){
for (var i=0;i<objSelect.options.length;i++){
if (objSelect.options[i].text.toUpperCase().indexOf(typed)==0){
objSelect.selectedIndex = i;

//remember selected index because in IE (only)...
//if the SELECT tag is "pulled-down" as you typed keys then press TAB key, it resets to the first option
//accessed and set in ONBLUR and ONCLICK event handlers of SELECT tag, respectively
index = i;

return false;
}
}
return false; //always return false
}

</script>
</head>
<body>
<form name="f">
<h3>Auto-selecting options based on continuous keystrokes</h3>

Set focus on the listbox below then type the desired letters:<br>
<select name="s" onkeydown="return s1(this,event)" onblur="this.selectedIndex=index;prev=index" onclick="index=this.selectedIndex;prev=index">
<option>Albania</option>
<option>Australia</option>
<option>Austria</option>
<option>Cambodia</option>
<option>Cameroon</option>
<option>Canada</option>
<option>China</option>
<option>England</option>
<option>Holland</option>
<option>Guatemala</option>
<option>Malaysia</option>
<option>Nigeria</option>
<option>Philippines</option>
<option>Uruguay</option>
</select>
<p>
<small>
<b>Note:</b><br>
- Press ESC key to reset keystrokes<br>
- Press BACKSPACE key to delete last keystroke<br>
- Look at status bar to see what you are typing<br>
- Works with IE and NS6
</form>
</body>
</html>

BubbleStar
09-19-2002, 06:32 AM
Hey thanx! :thumbsup:

What if i want to allow the user to press either the enter or tab key after they had made their selection in the combo box? do i just change the code so that it'll "listen" to the enter key?

another problem i faced is that, the codes dun actually work when there are numbers or other characters like "(,. " in the option..
so, how to i correct the codes to make it accept other characters other than just alphabeths??

thanx for e help..
:o

glenngv
09-19-2002, 07:17 AM
Originally posted by BubbleStar
Hey thanx! :thumbsup:

What if i want to allow the user to press either the enter or tab key after they had made their selection in the combo box? do i just change the code so that it'll "listen" to the enter key?

another problem i faced is that, the codes dun actually work when there are numbers or other characters like "(,. " in the option..
so, how to i correct the codes to make it accept other characters other than just alphabeths??

thanx for e help..
:o

for the ENTER key

Change the line:

else if (code==9){ //TAB key

to:

else if (code==9 || code==13){ //TAB or ENTER key


for the special chars, numbers work but not parenthesis, comma and period. i'll try to fix it later. Are there any chars you wanted?

BubbleStar
09-19-2002, 07:22 AM
thanks

characters i wanted are ()/.&,-@[]

adios
09-19-2002, 07:25 AM
Observing with interest...very nice work. :D

Upgraded my own; still a work-in-progress but interesting. Note: the 'assisting' textbox is in its own form and won't add an additional field to the main form. Anyway...


<html>
<head>
<title>untitled</title>
<style type="text/css">

form {
margin: 0px;
}

input {
font: 200 11px "comic sans ms";
color: #ffffff;
border: 1px solid #a0e0e0;
background: #000000;
}

select {
font: 200 11px "comic sans ms";
color: #ffffff;
background: #248080;
}

#box {
position: absolute;
width: 140px;
border-width: 4px;
border-color: #248080;
border-style: outset inset inset outset;
}

#placeholder {
position: relative;
width: 136px;
height: 53px;
text-align: left;
}

body {
text-align: center;
margin-top: 10%;
background: #333f40;
}

</style>
<script type="text/javascript" language="javascript">

function setSelect(e, field, s) {
if (typeof setSelect.fieldLength == 'undefined') setSelect.fieldLength = field.value.length;
if (field.value.length<setSelect.fieldLength) {
setSelect.fieldLength = 0;
field.value = '';
return false;
}
var val = field.value.toLowerCase();
var opt = 1, firstone, howmany = 0;
while (s.options[opt] && s.options[opt].value.substring(0,val.length).toLowerCase() != val)
++opt;
firstone = opt;
while (s.options[opt] && s.options[opt++].value.substring(0,val.length).toLowerCase() == val)
++howmany;
if (!howmany) {
alert('That entry does not exist.');
field.value = field.value.substring(0, field.value.length - 1);
field.focus();
return false;
}
s.size = 1;
s.selectedIndex = firstone;
s.size = howmany;
if (howmany == 1) {
field.value = s.options[s.selectedIndex].value;
field.style.color = '#ffff00';
setSelect.fieldLength = field.value.length;
return false;
}
else field.style.color = '#ffffff';
return false;
}

function fillit(str) {
var el = document.forms[0].country;
el.value = str;
el.style.color = '#ffff00';
return false;
}

onload = function() {
document.forms[0].reset();
document.forms[1].reset();
document.forms[0].country.select();
}

</script>
</head>
<body>
<div id="placeholder">
<div id="box">
<form>
<input name="country" type="text" value="Enter Country" style="width:136px;text-align:center;"
onkeyup="if(this.value)setSelect(event,this,f1.countries)">
</form>
<form name="f1">
<select name="countries" size="1" onchange="fillit(this[this.selectedIndex].value)">
<option value="">|||||||||||||||||||||||||||</option>
<option value="Albania">Albania</option>
<option value="Andorra">Andorra</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Cambodia">Cambodia</option>
<option value="Cameroon">Cameroon</option>
<option value="Canada">Canada</option>
<option value="China">China</option>
<option value="England">England</option>
<option value="Eritrea">Eritrea</option>
<option value="Ethiopia">Ethiopia</option>
<option value="Finland">Finland</option>
<option value="France">France</option>
<option value="Gambia">Gambia</option>
<option value="Germany">Germany</option>
<option value="Greece">Greece</option>
<option value="Namibia">Namibia</option>
<option value="Nicaragua">Nicaragua</option>
<option value="Nigeria">Nigeria</option>
<option value="Norway">Norway</option>
<option value="Pakistan">Pakistan</option>
<option value="Peru">Peru</option>
<option value="Philippines">Philippines</option>
<option value="Poland">Poland</option>
<option value="Portugal">Portugal</option>
<option value="Spain">Spain</option>
<option value="Swaziland">Swaziland</option>
<option value="Sweden">Sweden</option>
<option value="Switzerland">Switzerland</option>
<option value="Nigeria">Nigeria</option>
<option value="Uganda">Uganda</option>
<option value="United States">United States</option>
<option value="United Arab Emirates">United Arab Emirates</option>
<option value="Uruguay">Uruguay</option>
</select>
</div></div>
<br>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br><br>
<input type="button" value="DONE"
onclick="alert(countries[countries.selectedIndex].value)">
</form>
</body>
</html>


adios

BubbleStar
09-19-2002, 07:31 AM
I also like to add that the numbers will be interpreted differently if u use the num lock keys..
:confused:

BubbleStar
09-23-2002, 03:06 AM
Hi..

i really need help here.. hope anyone wif any suggestion would reply..

Regarding the codes previously given by glenn, it is near perfection, but now, how do i change it to accept other characters like ()/.&,-@[] ?
i also realised that the codes doesn't accept numbers from the num lock keys..
should i detect the ascii code for each symbol i want to use or is there any other ways?
can someone show me??

thanx a lot..
:confused:

whammy
11-01-2002, 03:02 PM
I need something like this, that a client is requesting (although I have tried to talk them out of it... :()

How could I modify your script, Glenn, to reset it once an option no longer matches what they are typing?

Say that they are typing NEV

I would like that to match "Nebraska" until they type the third letter, in which case it will then select "Nevada", (which of course alerady works) - however then if they decide "oops" and try typing "Neb" again, it will be reset since "NevNeb" doesn't match anything in the dropdown), when I'd like to reset the selectedIndex to 1 and clear the string that's holding the characters.

Basically the problem with every script I'm finding is if they type something wrong, then the focus isn't reset. And of course this client doesn't want "instructions" on the page...

I know, they shouldn't be doing this in the first place. I am still trying to talk them out of it. :|

beetle
11-01-2002, 03:17 PM
Anyone ever seen this combo box (http://www.webfx.eae.net/dhtml/combobox/combobox.htm) ??

whammy
11-01-2002, 11:29 PM
Geez, something like that would be perfect if it also worked in Netscape 6+. Although I may just put an IE check in there and be done with it.

I'm going to look at the code and see if I can modify it to suit my needs. :)

glenngv
11-06-2002, 01:45 AM
whammy, you can use backspace to delete previous characters or ESC key to delete all characters.
here's the latest code (http://www21.brinkster.com/gver/javascript/multi-stroke_auto-select.htm)

whammy
11-06-2002, 01:50 AM
Well, I'll be darned. That's about as close as I would expect anyone to get.

You are a freaking genius. I love you. (Not really, I don't love YOU, I love your brain. ;))

Dude, I hope you are doing well in the Philippines, you would probably make a lot more money in the USA. :D

glenngv
11-06-2002, 02:08 AM
thanks whammy! :)
well, maybe you could recommend me to your boss :D
I would definitely want to go there as my colleagues and friends are there now. :)

whammy
11-06-2002, 02:10 AM
If you don't have any certifications, I'd get some if I were you (I'm hoping to work on that myself shortly!). You could probably land any job you wanted in the USA, and have the company sponsor your visa to boot.

I wish you were working for the company I work for, except I'd probably bother you too much. And they would not even come close to being able to pay you what you're worth. No chance. Although if you're cool with that, I would recommend you to ANYONE wholeheartedly.

;)

glenngv
11-06-2002, 03:26 AM
Fixed some bug when pressing BACKSPACE key.
When deleting the last character, the select element always resets to first item not to the previously selected item. This should have the same effect when pressing ESC key which deletes all characters typed.

here's the modified code (http://www21.brinkster.com/gver/javascript/multi-stroke_auto-select.htm).

To test it, do this:

Type AUST, that will select Australia. Then remove focus to the select element.
Then focus again, and type CAMER. That will select Cameroon.
Then delete each character by pressing BACKSPACE key until there are no more characters. After that, it should re-select the previous item which is Australia.

blueberry80
03-02-2004, 06:58 AM
Hi adios,
like to enquire for your search box and the dropdown list. If i am to search by typing in 'C' into the textbook , it will list out the whole range . Currently if the user clicks on the list the combo box will still remain and it will not become a dropdown list. What do i need to change for your code to enable me to become a dropdown list again once i selected my value from the list in the combo box.

Thanks

glenngv
03-02-2004, 07:11 AM
This thread is gathering dust...:D
Anyway, the links in my posts are now broken as I got problems with my host. If you're interested in the upgraded version of my script you found in this thread, you can checkout the Editable Type-ahead combo script in my sig.

beetle
03-02-2004, 07:23 AM
Seriously. 9/16/02?? That's like, what... 18 months?

blueberry80
03-02-2004, 07:51 AM
This is what i copied from the thread:

like to enquire for your search box and the dropdown list. If i am to search by typing in 'C' into the textbook , it will list out the whole range . Currently if the user clicks on the list the combo box will still remain and it will not become a dropdown list. What do i need to change for your code to enable me to become a dropdown list again once i selected my value from the list in the combo box.

Thanks





<html>
<head>
<title>untitled</title>
<style type="text/css">

form {
margin: 0px;
}

input {
font: 200 11px "comic sans ms";
color: #ffffff;
border: 1px solid #a0e0e0;
background: #000000;
}

select {
font: 200 11px "comic sans ms";
color: #ffffff;
background: #248080;
}

#box {
position: absolute;
width: 140px;
border-width: 4px;
border-color: #248080;
border-style: outset inset inset outset;
}

#placeholder {
position: relative;
width: 136px;
height: 53px;
text-align: left;
}

body {
text-align: center;
margin-top: 10%;
background: #333f40;
}

</style>
<script type="text/javascript" language="javascript">

function setSelect(e, field, s) {
if (typeof setSelect.fieldLength == 'undefined') setSelect.fieldLength = field.value.length;
if (field.value.length<setSelect.fieldLength) {
setSelect.fieldLength = 0;
field.value = '';
return false;
}
var val = field.value.toLowerCase();
var opt = 1, firstone, howmany = 0;
while (s.options[opt] && s.options[opt].value.substring(0,val.length).toLowerCase() != val)
++opt;
firstone = opt;
while (s.options[opt] && s.options[opt++].value.substring(0,val.length).toLowerCase() == val)
++howmany;
if (!howmany) {
alert('That entry does not exist.');
field.value = field.value.substring(0, field.value.length - 1);
field.focus();
return false;
}
s.size = 1;
s.selectedIndex = firstone;
s.size = howmany;
if (howmany == 1) {
field.value = s.options[s.selectedIndex].value;
field.style.color = '#ffff00';
setSelect.fieldLength = field.value.length;
return false;
}
else field.style.color = '#ffffff';
return false;
}

function fillit(str) {
var el = document.forms[0].country;
el.value = str;
el.style.color = '#ffff00';
return false;
}

onload = function() {
document.forms[0].reset();
document.forms[1].reset();
document.forms[0].country.select();
}

</script>
</head>
<body>
<div id="placeholder">
<div id="box">
<form>
<input name="country" type="text" value="Enter Country" style="width:136px;text-align:center;"
onkeyup="if(this.value)setSelect(event,this,f1.countries)">
</form>
<form name="f1">
<select name="countries" size="1" onchange="fillit(this[this.selectedIndex].value)">
<option value="">|||||||||||||||||||||||||||</option>
<option value="Albania">Albania</option>
<option value="Andorra">Andorra</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Cambodia">Cambodia</option>
<option value="Cameroon">Cameroon</option>
<option value="Canada">Canada</option>
<option value="China">China</option>
<option value="England">England</option>
<option value="Eritrea">Eritrea</option>
<option value="Ethiopia">Ethiopia</option>
<option value="Finland">Finland</option>
<option value="France">France</option>
<option value="Gambia">Gambia</option>
<option value="Germany">Germany</option>
<option value="Greece">Greece</option>
<option value="Namibia">Namibia</option>
<option value="Nicaragua">Nicaragua</option>
<option value="Nigeria">Nigeria</option>
<option value="Norway">Norway</option>
<option value="Pakistan">Pakistan</option>
<option value="Peru">Peru</option>
<option value="Philippines">Philippines</option>
<option value="Poland">Poland</option>
<option value="Portugal">Portugal</option>
<option value="Spain">Spain</option>
<option value="Swaziland">Swaziland</option>
<option value="Sweden">Sweden</option>
<option value="Switzerland">Switzerland</option>
<option value="Nigeria">Nigeria</option>
<option value="Uganda">Uganda</option>
<option value="United States">United States</option>
<option value="United Arab Emirates">United Arab Emirates</option>
<option value="Uruguay">Uruguay</option>
</select>
</div></div>
<br>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br><br>
<input type="button" value="DONE"
onclick="alert(countries[countries.selectedIndex].value)">
</form>
</body>
</html>

glenngv
03-02-2004, 08:03 AM
adios is currently inactive in CF forums

adios
04-13-2004, 01:22 AM
Off looking for WMD. Hold my calls. :confused:

stefferson
05-12-2004, 09:32 AM
Made some small changes, try this:

<html>
<head>
<script language="javascript">

var typed="";
var obj; //object handle to the select tag;

function select1(objSelect,e){
var code=(document.all) ? event.keyCode:e.which;
if (code==27){ //ESC key
typed="";
document.form.printCountry.value="";
objSelect.selectedIndex=0;
return;
}
var letter = String.fromCharCode(code);
typed+=letter;
//window.status=typed;
document.form.printCountry.value=typed;
obj=objSelect;
setTimeout("select2()",100);//put some delay because select's behavior overrides select2() function.
}

function select2(){
for (var i=0;i<obj.options.length;i++){
if (obj.options[i].text.toLowerCase().indexOf(typed.toLowerCase())==0){
obj.selectedIndex=i;
return;
}
}
obj.selectedIndex=0;
}

</script>
<style>
.hiddentextfield {
font-family: Arial;
COLOR: #000000;
BACKGROUND-COLOR: #FFFFFF;
font-size:12px;
text-align:right;
BORDER-STYLE: none;
}
</style>
</head>
<body>
<form name="form">
<select name="s" onkeydown="select1(this,event)">
<option>Albania</option>
<option>Australia</option>
<option>Austria</option>
<option>Cambodia</option>
<option>Cameroon</option>
<option>Canada</option>
<option>China</option>
<option>England</option>
<option>Eritrea</option>
<option>Uruguay</option>
</select>
<input name="printCountry" type="text" size="20" value="" class="hiddentextfield">
</form>
</body>
</html>

glenngv
05-12-2004, 10:09 AM
What are the changes for?
FYI, the script has been modified a long time ago to make it more robust. Checkout the "Editable Type-ahead Combo" link in my sig.

Anjali
09-30-2004, 08:09 AM
Hi!
I have a drop down list that I am populating from a database.
I have used your code (thanx :thumbsup: ) to enable the user to select an item from the list using more than one character.
Now, my problem is that on selected index changed of this list, I have to populate another drop down list.
For this I need to set the autopostback property of dropdownlist to true.
But if I do this, the list posts back on entry of the first character itself & selection of list item on entry of multiple characters is not possible anymore.
Can someone pleeease help me?? :confused:

glenngv
10-01-2004, 02:40 AM
Are you using the "Editable Type-ahead Combo" script? The script only fires onchange event when you press TAB or ENTER key inside the dropdown or of course when you select item normally.

Anjali
10-01-2004, 05:22 AM
Hi!
I can't seem to be able to access ur code on the editable type-ahead combo link. :( It keeps saying that the page can not be found. :confused:
The thread in which "Auto-selecting options based on continuous keystrokes" are discussed opens, but from there the link to brinkster.com does not open.
So, I used the code on this thread, i.e. the one you wrote for bubble star.
Can you maybe mail the code to me or post it in this thread??? :confused:
Thanx :)

glenngv
10-01-2004, 08:55 AM
The "Editable Type-ahead Combo" link in my sig links to the post where the latest code is attached. The post (2nd to the last post at the bottom of that thread) was posted by Choopernickel who did the enhancements of my original script.

Anjali
10-01-2004, 10:50 AM
Hi!
Thanks, I managed to find the code :)
But, I still have a problem :(
If I am populating the dropdownlist from a database and then calling your function, it does not seem to be working.
But if i hard code the drop down list options, then it works fine :confused:
Secondly, the type ahead combo fill function seems kinda scary and i can't understand how to pass it to my drop down list in the code behind.

Anjali
10-01-2004, 11:47 AM
Hi!
I have managed to figure out how to pass the drop down list to the function.
But the other problem still persists :(
If I use a drop down list being populated from a database, the code just doesn't seem to work. :(

glenngv
10-01-2004, 12:27 PM
Can you post or attach the code behind and the generated html?

Anjali
10-01-2004, 06:12 PM
Hi!
I dont have my exact code right now.
I'll post it on monday.
But this is what I am basically doing:

In page load:
obj.openconnection()
fill(dropdownlist)
obj.closeconnection()

In the fill function:
Dim objConn as New OleDbConnection(sConnStr)
'Create a command object for the query
Const strSQL as String = "SELECT DepartmentID, Name " & _
"FROM tblDepartment"
Dim objCmd as New OleDbCommand(strSQL, objConn)
'Create/Populate the DataReader
Dim objDR as OleDbDataReader
objDR = objCmd.ExecuteReader()
' code for binding datareader to dropdownlist.

This is basically the code I am using to populate the dropdownlist.
If i write the list options in the html, ur code works absolutely fine.
But on loading the options from a database table like this, the list behaves in the default way and it is not possible to select using multiple characters :(

glenngv
10-04-2004, 03:13 AM
Just make sure the code behind generates the dropdown with the same markup as how you would hardcode it in HTML.

Anjali
10-04-2004, 05:00 AM
Hi!
Thanx. Will try your suggestion and get back to you.

Anjali
10-06-2004, 03:33 PM
Finally managed to find the problem :thumbsup:
Nothing wrong with your code. :)
There seemed to have been some kind of problem with the way the dropdownlist was being populated from the database.
Thanx anyways. :)

iota
09-30-2005, 01:07 PM
The more the options value, the slower the effect.