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

AddThis

Related podcasts

  • Episode 6: Sizzling Open FX

    I have moved to Mozilla along with my long time friend cohort and collaborator, Ben Galbraith. He joins us on this podcast, along with Alex, who joins us as a Noogler.&#10;You can download the podcast directly (OGG format too), or subscribe to the series, including via iTunes).&#10;We get back in...

Events coming up

  • Apr 8

    jQuery - An Introduction

    Edinburgh, United Kingdom

    Abstract A new client-side framework has been taking the internet by storm, many have already been mesmerised by the power of such a small javascript library. And "what is the name of this fantastic framework?" I hear you ask, "jQuery" is my reply. jQuery is a robust javascript framework with a very small footprint (15kb minified and GZipped) which makes the complicated aspects of javascript very simple. From traversing the Document Object Model to complex AJAX functionality, jQuery can do i...