»
EnglishFrenchVietnamese

Print - Adding Options - JavaScriptBank.com

Full version: jsB@nk » Form » Adding Options
URL: https://www.javascriptbank.com/adding-options.html

Adding Options © JavaScriptBank.comUse this JavaScript to add or change options on a form select menu. Clean and simple. Includes comments.

Full version: jsB@nk » Form » Adding Options
URL: https://www.javascriptbank.com/adding-options.html



JavaScript
<script type="text/javascript"><!-- Begin// Created by: Rick Johnson :: http://www.vistaviews.net/myPage/function buildSelect(x) { //alert(x) ; //returnif (x == "12") {   /* set choices 1 and 2 */  option0 = new Option("Please Select",0)  option1 = new Option("Value 1",1)  option2 = new Option("Value 2",2)  // write them  document.form1.fieldB.options[0] = option0  document.form1.fieldB.options[1] = option1  document.form1.fieldB.options[2] = option2  return;  }if (x == "34") {   /* set choices 3 and 4 */   option0 = new Option("Please Select",0)  option1 = new Option("Value 3",1)  option2 = new Option("Value 4",2)  // write them  document.form1.fieldB.options[0] = option0  document.form1.fieldB.options[1] = option1  document.form1.fieldB.options[2] = option2  return;  }if (x == "00") {   /* set choices to none */   option0 = new Option("None",0)  option1 = new Option("",1)  option2 = new Option("",2)  // write them  document.form1.fieldB.options[0] = option0  document.form1.fieldB.options[1] = option1  document.form1.fieldB.options[2] = option2  return;  } }// End --></script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<form name="form1"><select name="fieldB"><option size="15"></select><br><input type="radio" name="choice" onclick="buildSelect(12)"> Add choices 1 and 2   <input type="radio" name="choice" onclick="buildSelect(34)"> Add choices 3 and 4<br><input type="radio" name="choice" onclick="buildSelect(00)">Clear all choices</form><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->