Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Trying to use .js file for dropdownlist autocomplete

I'm trying to use a .js file to get autocomplete on a dropdownlist control in my ASP.NET application, but keep getting a syntax error.

The error on the .js file is:

Line 15, Char 1, Object Expected:

var keys;
var timeStamp;
timeStamp = new Date();
function dd_onkeypress(DropDownList1) {
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<BR>
if (diff > 1000) {
keys = key;
} else {
keys = keys + key;
}
var cnt;
for (cnt=0;cnt<document.all
(DropDownList1).children.length;cnt++) {
var itm = document.all(DropDownList1).children[cnt].text;
if (itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase())
{
document.getElementById(DropDownList1).selectedIndex = cnt;
break;
}
}
}
//document.all(DropDownList1).onchange();
}

In my Page_Load .aspx page, I've got:
DropDownList1.Attributes.Add("onkeyup", "javascript:return dd_onkeypress(this);")
[1789 byte] By [bubberz] at [2007-11-11 6:41:39]
# 1 Re: Trying to use .js file for dropdownlist autocomplete
keys is never defined as an object type

so if it gets to this line
keys = keys + key;
it has no value to + key upon.

perhaps just doing:
var keys = '';
could help your code.
KC-Luck at 2007-11-11 23:35:07 >
# 2 Re: Trying to use .js file for dropdownlist autocomplete
KC-Luck,

We found that the line it's pointing to is actually the line in the HTML code for the .aspx page. If we move the asp:dropdownlist tag down a line, then the error line goes to line 16.
bubberz at 2007-11-11 23:36:08 >
# 3 Re: Trying to use .js file for dropdownlist autocomplete
right on, plus on second look it appears keys would always become defined, unless the user clicked upon before the first 1 second :eek:
any link to the rendered page?
KC-Luck at 2007-11-11 23:37:17 >
# 4 Re: Trying to use .js file for dropdownlist autocomplete
Nah, it's pretty secure. I wish since this is killin' me! I still get an "Object Expected" on the line that points to the dropdownlist in the HTML.

In the Page_Load:
DropDownList1.Attributes.Add("onkeypress", "javascript:dd_onkeypress(this);return false;")

Then .js file is:
var keys;
var timeStamp;
timeStamp = new Date();
function dd_onkeypress(DropDownList1) {
* 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<BR>***
* ********if (diff > 1000) {
* ********************keys = key;****
* *********************} else {
* *********************keys = keys + key;
* *********************}
* *********************var cnt;
* *********************//for (cnt=0;cnt<document.all(DropDownList1).children.length;cnt++)
* *********************//for (cnt=0;cnt<DropDownList1.children.length;cnt++)
* *********************for (cnt=0;cnt<DropDownList1.options.length;cnt++) {
********************** var itm = DropDownList1.options[cnt].text;
********************** if
(itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase())
* *********************{
* *********************//var itm = document.all(DropDownList1).children[cnt].text;
* *********************//var itm = DropDownList1.children[cnt].text;
* *********************//if (itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase())
{
//document.getElementById(DropDownList1).selectedIndex = cnt;
DropDownList1.selectedIndex = cnt;
break;
}
}
}
//document.all(DropDownList1).onchange();
}
bubberz at 2007-11-11 23:38:17 >
# 5 Re: Trying to use .js file for dropdownlist autocomplete
will it run without the javascript: put in before the dd_...
KC-Luck at 2007-11-11 23:39:12 >
# 6 Re: Trying to use .js file for dropdownlist autocomplete
If I comment out the Page_Lode code which calls the .js file, yeah, it just acts as normal picking up the first letter of the entry. Typing with the intention of "Abraham" takes me to "Aalcot" then "Babylon" or something along those lines. Not to Abraham.
bubberz at 2007-11-11 23:40:11 >
# 7 Re: Trying to use .js file for dropdownlist autocomplete
sorry not following that your Page_Load executes that .js file above?
you shouldn't need the javascript: beginning of Attributes.Add tried it here successfully.

onload for DropDownList1
DropDownList1.Attributes.Add("onkeypress","alert(event.keyCode)");

and the lines with document.all(DropDownList1)... don't need the document.all()..

furthermore, i would replace the argument name with just list or something,
and then list would already be the valid reference to your DropDownList1,
sometimes IE doesn't like you to have/create variable of same element ID.
KC-Luck at 2007-11-11 23:41:15 >
# 8 Re: Trying to use .js file for dropdownlist autocomplete
var keys = '', timeStamp = new Date();

function dd_onkeypress(list) {
var i = 0, key = window.event.keyCode;
window.event.returnValue = false;
if((key>=97 && key<=122)||(key>=65 && key<=90)||(key>=48 && key<=57)){
key = String.fromCharCode(key);
var diff = (new Date).getTime()-timeStamp.getTime();
keys = keys + (diff > 1000 ? '' : key);
timeStamp = (new Date);
}
var c = list.childNodes;
for(i = 0; i < c.length; i++) {
var s = (c[i].text||c[i].innerText);
if(s.substring(0,keys.length).toLowerCase()==keys.toLowerCase()){
list.selectedIndex = i;
break;
}
}
}
KC-Luck at 2007-11-11 23:42:13 >