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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdownlist to respond to alpha key strokes

Status
Not open for further replies.

bubberz

Programmer
Dec 23, 2004
117
US
I have a dropdownlist with over 300 last names. If I wanted to go to the last name of "Clinton", I can press "C" and it will go to the first value starting with "C", such as "Caanton". Once I press "l", then it goes to the first value that starts with "L" like the last name of "Lake". Is there a way to have the key strokes go left to right in the dropdownlist value rather than going to a new alphabetical grouping?
 
Yes but you'll have to extend the control yourself.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
The Javascript
Code:
var keys;
var timeStamp;
timeStamp = new Date();

function dd_onkeypress(ddListName) {
 var key = event.keyCode;
 event.returnValue=false;
	
 //a-z, A-Z, 0-9
 if ((key>=97 && key<=122) || (key>=65 && key<=90) || (key>=48 && key<=57)) {
   key = String.fromCharCode(key);
   var now = new Date();
   var diff = (now.getTime() - timeStamp.getTime());
   timeStamp = new Date();
   //1 seconds = 1000 milliseconds
   if (diff > 1000) {
     keys = key;	
   } else {
     keys = keys + key;
   }
   var cnt;
   for (cnt=0;cnt<document.all (ddListName).children.length;cnt++) {
     var itm = document.all(ddListName).children[cnt].text;
     if (itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase()) {
     document.all(ddListName).selectedIndex = cnt;
       break;
     }
}
}
document.all(ddListName).onchange();
}

The Code Behind
Code:
Me.dropdown.Attributes.Add("onkeypress", "javascript:return dd_onkeypress('dropdown');")

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Checkai - I get a javascript error with that code.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
there could be a missed } in the copy, but I use it and it works quite well...let me check again...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
var keys;
var timeStamp;
timeStamp = new Date();

function dd_onkeypress(ddListName) {
var key = event.keyCode;
event.returnValue=false;

//a-z, A-Z, 0-9
if ((key>=97 && key<=122) || (key>=65 && key<=90) || (key>=48 && key<=57)) {
key = String.fromCharCode(key);
var now = new Date();
var diff = (now.getTime() - timeStamp.getTime());
timeStamp = new Date();
//1 seconds = 1000 milliseconds
if (diff > 1000) {
keys = key;
} else {
keys = keys + key;
}
var cnt;
for (cnt=0;cnt<document.all(ddListName).children.length;cnt++) {
var itm = document.all(ddListName).children[cnt].text;
if (itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase()) {
document.all(ddListName).selectedIndex = cnt;
break;
}
}
}
document.all(ddListName).onchange();
}

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
OK - if it helps I think it's on the line:
Code:
for (cnt=0;cnt<document.all (ddListName).children.length;cnt++) {
or at least that's where Internet Explorer thinks it is!

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Still getting an error here...

Also, another javascript alternative (using regex) can be found at:

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top