Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 24th, 2005, 11:17 AM
Registered User
 
Join Date: Jun 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trying to use .js file for dropdownlist autocomple

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).selectedInd ex = cnt;
                       break;
                       }
                      }
                     }
                   //document.all(DropDownList1).onchange();
                   }

In my Page_Load .aspx page, I've got:
 DropDownList1.Attributes.Add("onkeyup", "javascript:return dd_onkeypress(this);")

 
Old August 25th, 2005, 04:41 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Bubberz!!

***************First Method************
<script>
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").selectedI ndex = cnt;
                       break;
                       }
                      }
                     }
                   //document.all("DropDownList1").onchange();
                   }
</script>
**************or**** use Second Method************


<script>
var keys;
var timeStamp;
timeStamp = new Date();
function dd_onkeypress(DropDownList1) {

DropDownList1=DropDownList1.id
  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.le ngth;cnt++) {

                       var itm = document.all(DropDownList1).children[cnt].text;
                       if (itm.substring(0,keys.length).toLowerCase()==keys. toLowerCase())
                       {
                       document.getElementById(DropDownList1).selectedInd ex = cnt;
                       break;
                       }
                      }
                     }
                   //document.all(DropDownList1).onchange();
                   }
</script>

**** NOtes*******

1>When you call dd_onkeypress(this) ,the receiving function get the object,
offcourse it has name/id so u need to convert it into corresponding id or name

2>if u know there is only one dropdownlist1 then use first method

since document.all(object)---> there must be document.all("dropdownlist1") or use second method

Hope this will help you


Cheers :)

vinod
 
Old August 25th, 2005, 12:56 PM
Registered User
 
Join Date: Jun 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just tried the first method, since I've only got on dropdownlist on the page, and get object expected on line 16, which is the line in the HTML code that looks to the asp:dropdownlist

 
Old August 27th, 2005, 11:13 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

please check the value of totallength in ur written function.function dd_onkeypress(DropDownList1)


totallength=document.all(DropDownList1).children.l ength
alert(totallength)

I think follwing will work fine-

totallength=document.all("DropDownList1").children .length
alert(totallength)






hope this will help you

Cheers :)

vinod





Similar Threads
Thread Thread Starter Forum Replies Last Post
To include a js file into another js file jdang67 Javascript 4 February 28th, 2008 03:32 AM
asp file into a javascript file (js)? jstewie Javascript How-To 2 August 25th, 2005 08:32 AM
convert a js file from a asp file - change format jstewie Javascript How-To 0 July 21st, 2005 08:57 AM
.js file question savoym Javascript How-To 1 May 17th, 2004 02:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.