Dynamically loading dropdown boxes

  • 14 years ago

    After much searching on the web I finally found someone who knew what they were talking about, and actually had correct code that WORKS!!! If you want to load a dropdown box depending on what the selection in a previous dropdown box was, this is how to do it......

    <%@LANGUAGE="Javascript"%>
    <html>
    <head>
    <title>Load Dropdown</title>
    <script language="JavaScript">
    <!--
    var tennisplayers = new Array("Safin","Andre Agassi","Pete Sampras","Anna Kournik","Martina Hingis");
    var cricketplayers = new Array("Sachin Tendulkar","Steve Waugh","Brian Lara","Sir Bradman");
    function set_player(){
      var select_sport= document.form1.sport;
      var select_player= document.form1.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],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],cricketplayers[i]);
      }

    -->
    </script>
    </head>
    <body>
    <form name="form1"><div align="center">Select Sport
      <select name="sport" onChange="set_player();">
        <option value="" selected="selected">Select Sport</option>
        <option value="tennis">Tennis</option>
        <option value="cricket">Cricket</option>
      </select> Select Player
      <select name="player">
        <option value="" selected="selected"></option>
      </select>
    </form>
    </body>
    </html>




































Post a reply

No one has replied yet! Why not be the first?

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

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan