JavaScript
  Home arrow JavaScript arrow Page 2 - Dynamically Populating Select Menus Client...
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 
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

Dynamically Populating Select Menus Client-Side
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2004-06-02

    Table of Contents:
  • Dynamically Populating Select Menus Client-Side
  • Code
  • Function
  • Conclusion

  • 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


    Dynamically Populating Select Menus Client-Side - Code


    (Page 2 of 4 )

    Before jumping into code, I'll explain to you by which mechanisms we can accomplish said task. Basically we're going to maintain a JavaScript array of all possible items that could be populated into the second menu. For each item in the array, there will also be indicated the item in the first menu with which it is associated. So then when the user selects a new item in the first list, we can take the new selected value, iterate through the array to find only those items associated with the value, and load them into the second menu. That means that until something is selected in the first menu, there will be nothing in the second.

    While this could be a somewhat simple idea and tutorial, I'm afraid I have to throw in a little something to de-simplify it all. Suppose you wish to implement this little gadget in an 'edit' screen. As in, you as an administrator want to edit the information about a particular laptop, and have this piece of JavaScript installed on the page. Now the laptop already belongs to a certain category and sub-category, so we'll have to have some sort of handler to pre-populate the menus based on the information that's already there. Lost? I really hope not, because we're about to start coding.

    Now we can do this all with one array and one function. In this example I'll show you how to create the array dynamically on the server, but first we need to see what it should look like statically. All will feed off a form that will look something like this:

    <form name="form1">

    <select name="types" onChange="fillItems(0)">

    <option value="32">Laptops</option>

    <option value="21">PDAs</option>

    </select>

    <select name="items"></select>

    </form>

    The array we will need will be multi-dimensional. In the first dimension, each item will contain an array with three indexes. The first will be the ID of the main category that it belongs to; in the example it will be either laptops (ID: 32) or PDAs (ID: 27). The second index will contain the actual unique ID of the item. The third will just be the description. In the end it should look like this:

    arItems = [

    [ 32, 234, 'Toshiba SuperLaptop'],

    [ 32, 156, 'Compaq WonderMachine'],

    [ 32, 333, 'Sony ThinBook'],

    [ 27, 656, 'IBM c6'],

    [ 27, 467, 'Palm HandThing']

    ]

    So that's the end product, now I'll show you how to generate it. I'm assuming that you'll retrieve a result set from a database with ASP.

    <script language="JavaScript">


    var arItems = new Array()

    arItems = [

    <%

    strSQL = "SELECT type_id, id, description FROM items"

    objRS.Open strSQL, strConn

    arItems = objRS.GetRows()

    objRS.Close()

    Now we dump the contents of the server-side array into the identically-named client-side array.

    for i = 0 to uBound( atItems, 2 )

    response.Write( "[" & atItems( 0, i ) & "," & atItems( 1, i ) & _

    ",'" & atItems( 2, i ) & "']" )

    if i < uBound( atItems, 2 ) then response.Write("," & vbCrLf )

    next

    %>

    ]

    With this mix of ASP and JavaScript, we should have the array we need. Feel free to use PHP, or whichever technology you prefer. Now let's get into the function.

    More JavaScript Articles
    More By Justin Cook


     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT