HTML and CSS Reference
In-Depth Information
To display the name of the first form, descend the JavaScript tree, starting at the
window , to the document , to the first form object named form1 , to its name property.
We could write document.forms[0].name or , but by using the
form object as a property of the document and the associative array notation with
its name in square brackets, it is easier to identify the object as a form object by
name if there are a number of forms on the page.
To display the name of the second form, descend the JavaScript tree as in line 8.
This time we left out the window object, which is fine because JavaScript knows
that the window is always at the top of the tree. See Figure 11.13.
Although this chapter focuses on the JavaScript legacy DOM 0 when working
with forms, all HTML elements are accessible via the DOM Level 1, the standard-
ized DOM, which all modern browsers should support. Navigating forms, images,
and links with JavaScript has always been supported by DOM 0 and is still widely
The getElementById() method returns a reference to the HTML element by the id
that was assigned to it on line 1. In most of the examples, the name and id are the
same, but in this example, they are different to make it clear that the id , not the
name , is used to get a reference to the element. The variable, f1 , is a reference to
the first form.
Now that we have a reference to the form, we can use it and the dot notation to
get its name.
Figure 11.13 Name those forms!
Search WWH ::

Custom Search