Advertisement
iamasgor

Dynamic Select Statements

Aug 22nd, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.63 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <head>
  3. <title>Dynamic Select Statements</title>
  4. <script type="text/javascript">
  5.  var countryLists = new Array(4)
  6.  countryLists["empty"] = ["Select a Country"];
  7.  countryLists["North America"] = ["Canada", "United States", "Mexico"];
  8.  countryLists["South America"] = ["Brazil", "Argentina", "Chile", "Ecuador"];
  9.  countryLists["Asia"] = ["Bangladesh", "Russia", "China", "Japan"];
  10.  countryLists["Europe"]= ["Britain", "France", "Spain", "Germany"];  
  11.  function countryChange(selectObj) {  
  12.  var idx = selectObj.selectedIndex;  
  13.  var which = selectObj.options[idx].value;
  14.  cList = countryLists[which];
  15.  var cSelect = document.getElementById("country");
  16.  var len=cSelect.options.length;
  17.  while (cSelect.options.length > 0) {
  18.  cSelect.remove(0);
  19.  }
  20.  var newOption;
  21.  for (var i=0; i<cList.length; i++) {
  22. newOption = document.createElement("option");
  23. newOption.value = cList[i];
  24. newOption.text=cList[i];
  25. try {
  26. cSelect.add(newOption);
  27. }
  28. catch (e) {
  29. cSelect.appendChild(newOption);
  30. }
  31. }
  32. }
  33. </script>
  34. </head>
  35. <body>
  36.   <h1>Dynamic Select Statements</h1>
  37.   <label for="continent">Select Continent</label>
  38.   <select id="continent" onchange="countryChange(this);">
  39.     <option value="empty">Select a Continent</option>
  40.     <option value="North America">North America</option>
  41.     <option value="South America">South America</option>
  42.     <option value="Asia">Asia</option>
  43.     <option value="Europe">Europe</option>
  44.   </select>
  45.   <br/>
  46.   <label for="country">Select a country</label>
  47.   <select id="country">
  48.     <option value="0">Select a country</option>
  49.   </select>
  50. </body>
  51. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement