Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Populating drop down lists from radio buttons


Message #1 by "PeaCey" <peacey_69@h...> on Thu, 21 Jun 2001 22:28:18 +0100
Hi,
This should help.
All it does is call the setOptions() function when the radio is clicked, 
then uses the appropriate index of the array.

---------------------------------------------------------------
<html>
<head>
<script>
arrText = new Array;  // arrays for text to show
arrText[0] = new Array("chinese", "burgers", "italian");
arrText[1] = new Array("classical", "pop", "jazz", "rock");
arrText[2] = new Array("America", "France", "UK", "Australia", "China");

arrValue = new Array;  // arrays for values of options
arrValue[0] = new Array("china", "junk", "italy");
arrValue[1] = new Array("classic", "pop", "jazz", "rock");
arrValue[2] = new Array("US", "Fr", "UK", "Au", "Ch");

// called when radio clicked
function setOptions(i)
{
  oSlt = document.all.sltBox; // link to drop-down list
  oSlt.options.length = 0;  // delete all previous options
  for(iOpt = 0; iOpt < arrText[i].length; iOpt++)
  {
    oSlt.options.length++;  // add an option and set values and text for 
each bit of array
    oSlt.options[oSlt.options.length - 1].text = arrText[i][iOpt];
    oSlt.options[oSlt.options.length - 1].value = arrValue[i][iOpt];
  }
}
</script>
</head>
<body>
<select id="sltBox"></select><br><br>
Food: <input type="radio" name="rad" onclick="setOptions(0)"><br>
Music: <input type="radio" name="rad" onclick="setOptions(1)"><br>
Places: <input type="radio" name="rad" onclick="setOptions(2)">
</body></html>
---------------------------------------------------------------

Hope it helps.
Philip

  Return to Index