HTML and CSS Reference
In-Depth Information
Figure 15.8
The Firefox DOM Inspector.
In the previous example, walking with the nodes was like walking through a maze in a
palace garden. Although the DOM is represented as a hierarchal tree of elements, where
each element is represented as a node, walking with the nodes can be daunting, so the
W3C provides additional methods and properties to help make the walk easier.
The DOM provides two methods,
getElementById()
and
getElementsByTag(),
to
directly access the target element you are trying to reach. It also provides properties to
represent attributes of an element listed in Table 15.5.
Using the
id
attribute is not a new idea. We have been using it throughout this text. All
browsers that comply with the W3C's DOM1+ should implement the
id
attribute for
accessing the elements in a document. It uniquely identifies any HTML element in a
Web document. Note: This is not the same as the
name
attribute discussed in Chapters
11 and 12. If you recall, the
name
attribute is used with forms, images, links, and
anchors. The
name
attribute does not have to be unique and is used by the browser to
create
name/value
pairs to be submitted to a server program as part of the URL (GET
method) or HTTP header (POST).
The HTML element's
id
attribute must be assigned a unique value that is used as a
CSS selector to identify a style rule or to get a reference to that element in a JavaScript