(Answer) (Category) Products of Aardvarks With Chisels : (Category) HTML : (Category) Javascript :
Modify select box elements
This is a little bit of code that sort of demonstrates how to modify the contents of a select box. Put a number in the maxvalue box, and the select box will be filled with numbers from 1 to maxvalue. If you select a value fin the select box, that value will be removed. If you specify a name and value in the name and value boxes, that name/value pair will be added to the select box.
http://www.cloudmaster.com/~sauer/demos/select_modify.html
<html>
<head><title>Adding stuff to a select box</title></head>

<script LANGUAGE="JavaScript">
function addOpt(daForm){
        var val = daForm.elements['VAL'].value;
        var nam = daForm.elements['NAM'].value;
        var newOpt = new Option(nam, val, false, false);
        var newIndex = daForm.elements['edthecat'].length;
        daForm.elements['edthecat'].options[newIndex] = newOpt;
}
function delOpt(E){
        var Esize = E.length;
        if(Esize>1){
                var it = E.selectedIndex;
                E.selectedIndex = it>0 ? it-1 : (it<Esize+1 ? it+1 : Esize);
                E.options[it] = null;
                //history.go(0);
        }
}
function populate(daForm){
        newArr = new Array();
        E = daForm.elements['edthecat'];
        for(var j=0; j<E.length; j++){ E.options[j] = null;     }
        for(var i=1; i<=parseInt(daForm.elements['MAX'].value); i++){
                newOpt = new Option(i, i, (!i), (!i));
                //newOpt = new Option(i, i, false, false);
                E.options[i-1] = newOpt;
        }
}
</SCRIPT>

<body>
<form NAME="jimmie">
<select NAME="edthecat" onChange="delOpt(this)">
<option VALUE="Forest">Forest</option>
</select>
value   <input TYPE="TEXT" NAME="VAL" SIZE="15" VALUE="new value">
name    <input TYPE="TEXT" NAME="NAM" SIZE="15" VALUE="new text">
<input TYPE="BUTTON" VALUE="addthis" OnClick="addOpt(this.form)">
<br>
<center>
maxval  <input TYPE="TEXT" NAME="MAX" SIZE="15" VALUE="max pop val">
<input TYPE="BUTTON" VALUE="popupate" OnClick="populate(this.form)">
</center>
</form>
</body>
[Append to This Answer]
2001-Nov-26 3:22pm
Previous: (Answer) checkbox.js
Next: (Answer) Netscape's ua.js
This document is: http://www.cloudmaster.com/~sauer/projects/index.cgi?file=115
[Search] [Appearance]
This is a Faq-O-Matic 2.719.
This FAQ administered by sauer@cloudmaster.com