Friday 10, February 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    Forum    |    About Us    |    Contact Us

  Add or Change Options on a form select field using javascript

This script can be used to add or change options on a form select menu.
Cut & Paste Script

<html>
<head>
<title>Add or Remove Options on a form Select Field</title>
<script>
function buildSelect(x) {
  if (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 == "1234") {
    /* set choices 1,2, 3, and 4 */
    option0 = new Option("  Please Select  ",0)
    option1 = new Option("Value 1",1)
    option2 = new Option("Value 2",2)
    option3 = new Option("Value 3",3)
    option4 = new Option("Value 4",4)
    // write them
    document.form1.fieldB.options[0] = option0
    document.form1.fieldB.options[1] = option1
    document.form1.fieldB.options[2] = option2
    document.form1.fieldB.options[3] = option3
    document.form1.fieldB.options[4] = option4
    return;
    }

  if (x == "00") {
    /* set choices to none */
    option0 = new Option("  None  ",0)
    option1 = new Option("",1)
    option2 = new Option("",2)
    option3 = new Option("",3)
    option4 = new Option("",4)
    // write them
    document.form1.fieldB.options[0] = option0
    document.form1.fieldB.options[1] = option1
    document.form1.fieldB.options[2] = option2
    document.form1.fieldB.options[3] = option3
    document.form1.fieldB.options[4] = option4
    return;
    }
}

</script>
</head>
<body>

<form name="form1">
<select name="fieldB">
<option size="15">  Select from below  </option>
</select>
<br>
<input type="radio" name="choice" onclick="buildSelect(12)">Add choices 1 and 2<br>
<input type="radio" name="choice" onclick="buildSelect(34)">Add choices 3 and 4<br>
<input type="radio" name="choice" onclick="buildSelect(1234)"> Add all choices<br>
<input type="radio" name="choice" onclick="buildSelect(00)">Clear all choices
</form>

</body>
</html>

 
   
     RELATED SCRIPTS
 
Accept Disclaimer Radio Button Example JavaScript
Confirm your visitor's approval by requiring their name before continuing on. Useful if you require your visitors to accept a disclaimer before downloading software or visiting a section of your members-only site. Could be used with a cookie....more »
Category : JavaScript Examples Hits : 925
   
popup window using javascript
This javascript can be used to create a popup window. It will display a mini popup window when page loads....more »
Category : JavaScript Examples Hits : 286
   
A sample JSON definition
JavaScript Object Notation, has become a popular alternative to XML for defining structured data using JavaScript.
In strict JSON:
  • All string values must be in double quotes (single quotes won't do).
  • The name portion of each name/value pair must also be ...more »
Category : JavaScript Examples Hits : 359
   
Add Input Fields Dynamically using JavaScript
Use this script to upload files. As a file is selected, another input field is automatically added....more »
Category : JavaScript Examples Hits : 415
   
JavaScript Sequential fly-in text script
This script flies in multiple text from the left edge of the window, one at a time in sequential order similar to a PowerPoint effect. It's versatile enough to handle any number of text (or other rich HTML like images!), and you can customize both the animation speed and pause between each messa...more »
Category : JavaScript Examples Hits : 491
   
   
SCRIPT CATEGORIES
 
  C Programs
C Programs scripts
  C++ Programs
C++ Programs scripts
  Java Programs
Java Programs scripts
  JavaScript Examples
JavaScript Examples scripts
  PHP Examples
PHP Examples scripts
  VBScript Examples
VBScript Examples scripts
 
 
 
POPULAR E-BOOKS
 
Download Landing Page Success Guide to Millions Ebook Landing Page Success Guide to Millions
   
Download Computer Basics Ebook Computer Basics
   
Download List Creation Secrets Leaked Chapters  Ebook List Creation Secrets Leaked Chapters
   
Download Golden Adsense Strategy  Ebook Golden Adsense Strategy
   
Download Excerpts from the Abstract Ebook Excerpts from the Abstract
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz