Library code snippets

Dynamically filling ComboBoxes

If you are developing a professional site for a company you would invariably come across a situation where you are expected to do the above. Remember that the power of this script becomes evident when you use Javascript along with some other dynamic language such as JSP or ASP. You could probably fill the arrays used in this script with some data fetched from a database relating to the particular user. When he selects an entry in the first dropdown menu, he is immediately presented with his relevant data in the second menu, instead of making another request to the server to fetch more data. These types of scripts are very useful when you have to allow a user select some thing from a general level to a more specific level. I mean each successive dropdown menu would be more and more specific choice. Read on to understand the entire thing.

<SCRIPT LANGUAGE = "JavaScript">
<!--

var tennisplayers= new Array("Safin", "Andre Agassi", "Pete Sampras", "Anna Kournikova", "Martina Hingis");
var cricketplayers = new Array("Sachin Tendulkar", "Steve Waugh", "Brian Lara", "Sir Don Bradman");

function set_player() {

 var select_sport = document.myform.sport;
var select_player = document.myform.player;
var selected_sport = select_sport.options[select_sport.selectedIndex].value;

select_player.options.length=0;
if (selected_sport == "tennis"){

   for(var i=0; i<tennisplayers.length; i++)  
   select_player.options[select_player.options.length] = new Option(tennisplayers[i]);
 }
if (selected_sport == "cricket"){

   for(var i=0; i<cricketplayers.length; i++)
     select_player.options[select_player.options.length] = new Option(cricketplayers[i]);
 }
}
-->
</SCRIPT>

<BODY>
<FORM NAME="myform" METHOD="POST">

Sport
<SELECT NAME="sport" onChange="set_player()">
<OPTION VALUE="tennis">-------
<OPTION VALUE="tennis">Tennis
<OPTION VALUE="cricket">Cricket
</SELECT>

Player
<SELECT NAME="player">
<OPTION>------
</SELECT>

</FORM>
</BODY>

Comments

  1. 05 Jan 2007 at 19:37

    The following code which I copied out of a javascript book is not working.  What am I doing wrong?

    Huh? [:^)]

    [<%@LANGUAGE="Javascript"%>
    <html>
    <head>
    <title>Load Dropdown</title>
    <script language="JavaScript">
    <!--
    var cities = new array(4);
    cities["Australia"] = ["Sydney", "Melbourne", "Canberra", "Perth", "Brisbane"];
    cities["France"] = ["Paris", "Lyons", "Nice", "Dijon"];
    cities["Japan"] = ["Tokyo", "Kyoto", "Osaka", "Nara"];
    cities["New Zealand"] = ["Auckland", "Wellington", "Christchurch", "Dunedin", "Queenstown"];
    Function removeOptions (City)
    {
      For (i=0; i< City.options.length; i++)
      City.options[i] = null;
    }
    funtion addOptions (cities, City)
    {
      var i=0;
      removeOptions (City);  //called function to clear out the options
      for (i=0; i < cities.length; i++)
        City[i] = new option (cities[i], cities[i]);  //I DON'T UNDERSTAND WHAT THE SYTAX TO THIS LINE SHOULD BE, I THINK IT'S INCORRECT.
    }
    -->
    </script>
    </head>
    <body>
    <h2>Vacation Chooser</h2>
    <form name="testform" id="testform">
    Country:
    <select name="country" id="country" onChange="addOptions(cities[this.options[this.selectedIndex].text],document.testform.city)">
      <option selected>Australia</option>
      <option>France</option>
      <option>Japan</option>
      <option>New Zealand</option>
    </select>
    City:
    <select name="city" id="city">
      <option>Sydney</option>
      <option>Melborne</option>
      <option>Canberra</option>
      <option>Perth</option>
      <option>Brisbane</option>
    </select> 
    </form>
    </body>
    </html>]













































  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Dynamically filling ComboBoxes.

Leave a comment

Sign in or Join us (it's free).

Kiran Pai
AddThis

Related podcasts

  • jQuery in ASP.NET

    In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, Scott Koon, and Steven Harman discuss Microsoft's jQuery in ASP.NET announcement.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are you loo...

Events coming up

Want to stay in touch with what's going on? Follow us on twitter!