JavaScript
  Home arrow JavaScript arrow Page 2 - Suggest As You Type
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Suggest As You Type
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    2004-04-19

    Table of Contents:
  • Suggest As You Type
  • The Plan
  • The Suggestion Function
  • The End

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Suggest As You Type - The Plan


    (Page 2 of 4 )

    Well, I guess before we jump into JavaScript code, I should explain quickly what we'll do, and how we'll do it. Basically, we'll have a long list of items within a <select> menu. Now, it would get unnecessarily complicated to catch our keystrokes within a menu. So what we'll do is add an input box to capture our keystrokes, and match the list items to what we type, as we type it.

    OK, so let's take quick inventory of what is needed. First, the <select> menu, full of all the choices. Now this menu can be either static or dynamically populated, because this whole operation is taking place on the client's browser.  We also need an input box to type in. We need the JavaScript function that handles the typed input matching. Also, I will throw in some optional in line scripting, to populate the text box with the text of a selected item. All right, we have our plan, so let's get to it.

    The Code

    First let's start off with our form elements, the input box and the select menu.


    <form name="form1">
    <input type="text" name="employeeName" onKeyUp="suggestName();" /><br />
    <select name="employeeList" size="6"   
    onClick
    ="javascript:form1.employeeName.value=this.options[options.selectedIndex].text;">
    <%
    strSQL    
    "SELECT employee_name FROM employees ORDER BY employee_name"
    set objRs 
    objConn.Execute(strSQL)
    while not objRs
    .EOF
      response
    .write("<option value=""" objRs(0) & """>")
      response
    .write(objRs(0) & "</option>" vbcrLf)
      objRs
    .MoveNext
    wend
    %>
    </select>
    </form>

    In case any of the above code is unfamiliar to you, I'll explain it for you. First of all we have our input box, and the property is set such that every time a key is released within the box, we go off the suggestName() function, to see if we can match what's now in the box.

    For the select menu, the in-line scripting simply states that each time you click on an item, we're going to send the text value of the selected item to the text box. This serves one purpose here, to show that we have chosen an item. The real usefulness of doing this though, is in the case where you're not depending on the actual text of the item (employee name in this case), but really on a unique identifier, such as employee number. We don't really need to see the employee number, but we can modify the select menu to hold the UID as the value of each item, and fill in a hidden field with that id when an item is clicked (selected). This really comes into play when you're trying to ensure referential integrity in a database, maybe using the employee number as a primary key.

    If that's similar to the situation you have, you can just modify the SQL statement to pull the fields you need, and append some nearly identical in-line scripting. What you will do however, is send the 'value' instead of the 'text' of the selected item, probably to a hidden field.

    More JavaScript Articles
    More By Justin Cook


     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT