Introduction to JavaScript

Using Arrays to Refer to Forms

Type something into the text boxes. Then use the first button to see the form element names, and the second button to see the form element values.

First Name:       Last Name:

<SCRIPT>

function DisplayElementNames() {
     for (i = 0; i < 4; i++) {
          alert('document.Customer.elements[' + i + '].name is\n\n"' +
                    document.Customer.elements[i].
name + '"');
     }
}

function DisplayElementValues() {
     for (i = 0; i < 4; i++) {
          alert('document.Customer.elements[' + i + '].value is\n\n"' +
                    document.Customer.elements[i].
value+ '"');
     }
}

</SCRIPT >

<FORM NAME="Customer">
    
First Name: <INPUT TYPE="text" NAME="Firstname">
    
Last Name:  <INPUT TYPE="text" NAME="Lastname">
     <INPUT TYPE="button" NAME="Btn1" VALUE="Display the elements names"
       onClick="DisplayElementNames()">
     <INPUT
TYPE="button" NAME="Btn2" VALUE="Display the elements values"
       onClick="DisplayElementValues()">
</FORM >