Previous Example-|-Next Example-|-Return to Chapter Listing

Example 17.11:
Changing Select Box Text

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

You can modify the contents of the Select box. Enter an index position in the text box (from 0 to 3), then click the button:

Change this index position: to the following text:
If you enter an index number over 3, you get an error. Take a look at Example 17-12 for a way to fix that.
These are the scripts we used. First, in the HEAD: <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- function ChangeItem(Myform) { var i = Myform.txtIndex.value Myform.lstOption.options[i].text = Myform.txtNewText.value } //--> </SCRIPT> Later in the Web page the function is called from this form: <FORM> <SELECT name="lstOption" SIZE=5> <OPTION>Text at Index=0 <OPTION>Text at Index=1 <OPTION>Text at Index=2 <OPTION>Whatever text you want </SELECT> <BR> Change this index position: <INPUT TYPE="text" NAME="txtIndex" VALUE="0" SIZE=2> to the following text: <INPUT TYPE="text" NAME="txtNewText"VALUE="Type your text here"SIZE=20> <BR> <INPUT TYPE="button" VALUE="Change text" onclick="ChangeItem(this.form)"> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing