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

Example 10.7:
Using Arrays to Refer to Form Elements


Here's the first form in the document (it doesn't do anything):
Here's the second form. The buttons use different methods of referring to the form element when getting the value held in the text box:





This is the script we used in the HEAD: <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- function TextBoxName() { alert("The text box contains this text: " + document.secondForm.Lname.value) } function TextBoxArray() { alert("The text box contains this text: " + document.secondForm.elements[3].value) } function TextBoxArray2() { alert("The text box contains this text: " + document.forms[1].elements[3].value) } //--> </script> Later in the page we have this form: <FORM NAME="secondForm"> <INPUT TYPE="button" NAME="But1" value="Display the text-box value using the form and text box names" onclick="TextBoxName()"><BR> <INPUT TYPE="button" NAME="But2" value="Display the text-box value using the form name and elements array" onclick="TextBoxArray()"><BR> <INPUT TYPE="button" NAME="But2" value="Display the text-box value using the forms and elements array" onclick="TextBoxArray2()"><BR> <INPUT TYPE="text" NAME="Lname" VALUE="Smith"> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing