JavaScript
  Home arrow JavaScript arrow Page 4 - 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 
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

Dynamically Populating Select Menus Client-Side
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    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 - Conclusion


    (Page 4 of 4 )

    If you find this function really handy, and want to reuse it over and over, don't fall into the trap of duplicating code. The best way to handle that is to pass in the select menus you're operating on via the argument list, and that way the same function can be used on lots of different menus throughout your application.

    I have one big recommendation for this script. When coding it into your page, put the code after the </html> tag. Not necessarily the entire function, but at least the array generating code. The reason for this is that the array can become quite large, and you don't want the generation thereof to slow down the loading of the rest of your page.

    I hope you enjoy this little tool. Speaking from experience – once you implement it, you won't be able to live without it. If you have problems, feel free to hop on the forums! By the way, I've tested it with all major current browsers, and have not encountered a single problem.

    Here's all the code together:

    <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()


    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

    %>

    ]


    function fillItems( intStart ) {

    var fTypes = document.form1.types

    var fItems = document.form1.items

    var a = atItems

    var b, c, d, intItem, intType


    if ( intStart > 0 ) {

    for ( b = 0; b < a.length; b++ ) {

    if ( a[b][1] == intStart ) {

    intType = a[b][0];

    }

    }

    for ( c = 0; c < fTypes.length; c++ ) {

    if ( fTypes.options[ c ].value == intType ) {

    fTypes.selectedIndex = c;

    }

    }

    }


    if ( intType == null ) {

    intType = fTypes.options[ fTypes.selectedIndex ].value

    }

    fItems.options.length = 0;

    for ( d = 0; d < a.length; d++ ) {

    if ( a[d][0] == intType ) {

    fItems.options[ fItems.options.length ] = new Option( a[d][2], a[d][1] ); // no line-break here

    }

    if ( a[d][1] == intStart ) {

    fItems.selectedIndex = fItems.options.length - 1;

    }

    }

    }

    </script>


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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 6 hosted by Hostway
    Stay green...Green IT