34,333
edits
Changes
→Accessing Objects in a Form
== Accessing Objects in a Form ==
As we mentioned in the previous section the Form object is really just a container which contains other objects (such as text fields and buttons). Later in the chapter we will learn how to create those objects inside the Form object. First, however, it is useful to explore how to access the oibjects in a Form.
One way to access the elements in a Form is to use the Form object's ''elements'' property. This is an array containing all the objects in a Form (see [[JavaScript Arrays]] for details). We can, therefore, access any element in the array by using the index (note that the objects in the array are in the order in whcih they are specified in the Form. Suppose we have a text object which is the first object inside a Form called myForm. We can access this object as follows:
<pre>
document.myForm.elements[0]
</pre>
A much easier approach is to access the object using the name that was assigned to it at creation. This is achieved using the getElementById(). Assuming in our first example the Text object was assigned a name of ''phoneNumber'' we could asssign the object to a variable as follows:
<pre>
phone = document.getElementById("phoneNumber");
</pre>
== The JavaScript Text Object ==