Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing

Example 17.12:
Adding Items to a Select Box

[This example works with Netscape Navigator 3. It will not work in Netscape Navigator 2 or Internet Explorer 3.]

You can add an item to the Select box by typing some text, specifying the index position at which you want to insert the text, then clicking the button:

Add the following text to the select box:
Add it at index number:

These are the scripts we used. First, in the HEAD: <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- function AddItem(Myform) { var sText = Myform.txtItem.value; var iIndex = Myform.txtIndex.value; NewItem = new Option(sText, iIndex, false, true); Myform.lstOption.options[iIndex]=NewItem; history.go(0); } //--> </SCRIPT> Later in the Web page the function is called from this form: <FORM> <SELECT name="lstOption" SIZE=4> <OPTION>Text at Index=0 <OPTION>Text at Index=1 <OPTION>Text at Index=2 </SELECT> <BR> Add the following text to the select box: <INPUT TYPE="text" NAME="txtItem" VALUE="Type your new text here" SIZE=25><BR> Add it at index number: <INPUT TYPE="text" NAME="txtIndex" VALUE="3" SIZE=2><br> <INPUT TYPE="button" VALUE="Add Item" onclick="AddItem(this.form)"> </FORM>
Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing