dynamically filling dropdown from another dropdown

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2006
Posts: 7
Reputation: DNRSmitty is an unknown quantity at this point 
Solved Threads: 0
DNRSmitty DNRSmitty is offline Offline
Newbie Poster

dynamically filling dropdown from another dropdown

 
0
  #1
Jan 16th, 2007
Here's how to dynamically fill a dropdown box depending on the selection of another dropdown box. I looked all over the web, and in this forum, finally I found someone who knew what they were talking about and actually had code that worked.
:lol:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <%@LANGUAGE="Javascript"%>
  3. <html>
  4. <head>
  5. <title>Load Dropdown</title>
  6. <script language="JavaScript">
  7. <!--
  8. var tennisplayers = new Array("Safin","Andre Agassi","Pete Sampras","Anna Kournik","Martina Hingis");
  9. var cricketplayers = new Array("Sachin Tendulkar","Steve Waugh","Brian Lara","Sir Bradman");
  10. function set_player(){
  11. var select_sport= document.form1.sport;
  12. var select_player= document.form1.player;
  13. var selected_sport= select_sport.options[select_sport.selectedIndex].value;
  14. select_player.options.length=0;
  15. if (selected_sport == "tennis"){
  16. for(var i=0; i < tennisplayers.length; i++)
  17. select_player.options[select_player.options.length] = new Option(tennisplayers[i],tennisplayers[i]);
  18. }
  19. if (selected_sport == "cricket"){
  20. for(var i=0; i < cricketplayers.length; i++)
  21. select_player.options[select_player.options.length] = new Option(cricketplayers[i],cricketplayers[i]);
  22. }
  23. }
  24. -->
  25. </script>
  26. </head>
  27. <body>
  28. <form name="form1"><div align="center">Select Sport
  29. <select name="sport" onChange="set_player();">
  30. <option value="" selected="selected">Select Sport</option>
  31. <option value="tennis">Tennis</option>
  32. <option value="cricket">Cricket</option>
  33. </select> Select Player
  34. <select name="player">
  35. <option value="" selected="selected"></option>
  36. </select>
  37. </form>
  38. </body>
  39. </html>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC