HTML and CSS Reference
form by its name.
can now reference the button by its name. The id attribute will not be used in this
example. It is here because it is common to use both a name and id for the form
which will be covered in detail in later chapters.
The input type is a button named button2 and assigned a value of blue .
The form ends here.
of the document object. Without naming the form, it would be referenced as doc-
The name assigned to the first button is displayed. Putting the name as a string in
square brackets (associative array) is a common approach to naming forms and
their elements. Without naming the form or the button, it would be referenced as
document.forms.elements.value . It is easy to make typos here. Spelling my-
form.name as myform1.name causes the output to be myform1 is undefined . See Fig-
ures 11.14 and 11.15.
Using the associative array notation, the name for the form is a string inserted in
the first set of brackets, and the second element, a string name for button1 is in-
serted in the second set of square brackets. We use the dot notation to retrieve the
value of the type property for the form's button.
Using the two-dimensional associative array notation, we retrieve the value that
was assigned to button1 , which is red .
Figure 11.14 Name that button!