944,017 Members | Top Members by Rank

Ad:
Jan 16th, 2007
0

dynamically filling dropdown from another dropdown

Expand Post »
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>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DNRSmitty is offline Offline
7 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Dani Webs Code?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: CSS & JS Menu Problem on IE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC