tags. Use of inline event handlers is not best practice, and is used here for illustrative purposes
only. Chapter 11, “JavaScript Events and the Browser,” introduces a better approach for attaching
events to HTML. DOM Level 0 is also known
DOM Level 0 was implemented before other formal specifications of the DOM. After DOM
Level 1 was specified, the previous technology related to document scripting was codified
(though not really in any formal way by any standards body) as the legacy DOM Level 0.
Today, every major browser supports DOM Level 0 components for downward compatibility.
The DOM Level 0 concentrated mainly on giving access to form elements, but it also incorporated providing access to links and images. Chapter 14, “Using JavaScript with Web Forms,”
covers forms and how you access them with the DOM. Rather than spend time on examples
of DOM Level 0, I concentrate on DOM Levels 1 and 2, which you’re more likely to use when
you program in JavaScript.
The W3C issued Level 1 of the DOM as a specification in 1998. Like the legacy DOM, Level
1 is supported, in various forms, by all the major browsers. Level 2 of the DOM was formally
released in 2000. Support of Level 2 DOM varies more widely between browsers. Truthfully,
support of all DOM levels varies from browser to browser and from version to version.
Versions of Windows Internet Explorer prior to version 9 claim to support the DOM, but they
do so differently from other browsers. As a result, you need to be aware that the DOM feature or function you’re using or attempting to use in your JavaScript code might not work in
Internet Explorer or might work only in Internet Explorer and nowhere else (and no, that’s
not acceptable). Windows Internet Explorer Version 9 is a step in the right direction, but you
still need to account for compatibility issues between browsers. Where applicable, I point out
the places where browsers implement the DOM differently and some workarounds for such
events.
The DOM represents HTML documents in a tree-like structure—or rather, an uprooted treelike structure—because the root of the tree is on top. For example, consider the simple HTML
document shown in Listing 10-1.
Listing 10-1 A simple HTML document.
Here's some text.
Here's more text.
Figure 10-1 shows the HTML from Listing 10-1 when viewed in the tree structure of the DOM.
Figure 10-1 A simple document represented as a tree structure.
element
shown in Listing 10-1. You can both retrieve and set these attributes using the DOM, as you
will see later in this chapter.
When working with the DOM, you should be aware of the distinction among retrieving elements, setting elements and items related to elements, and removing or deleting elements.
The methods for working with DOM elements reflect this distinction.
Working with Nodes
The elements within the tree structure are sometimes referred to as nodes or node objects.
Nodes at the level below a given node are known as children. For example, in the structure
shown in Figure 10-1, the body node has three child nodes, all p elements, and one of the
p elements has a child of its own, an a element. The body node is said to be a parent of the p
nodes. Any nodes under a given node are known as descendants of that node. The three
p nodes in Figure 10-1 are known as siblings because they’re on the same level.
In the same way you use methods to work with elements of the DOM, you use methods to
work with nodes that reflect the parent/child and sibling relationships. For example, you can
use methods such as appendChild(), shown later in this chapter, to add nodes to an existing
parent.
Retrieving Elements
Retrieving the elements of a document is an essential way you use the DOM when programming with JavaScript. This section examines two of the primary methods you use to retrieve
elements: getElementById() and getElementsByTagName().
Retrieving Elements by ID
The getElementById() method is a workhorse method of the DOM. It retrieves a specified
element of the HTML document and returns a reference to it. To retrieve an element, it must
have an id attribute. For example, you can modify the HTML from Listing 10-1 to add an id
attribute to the a element, as shown in boldface here:
Hello World
Here's some text.
Here's more text.
Link to the W3
Chapter 10 The Document Object Model
207
Now that the a element has an id, you can retrieve it using the getElementById() method,
as follows:
var a1 = document.getElementById("w3link");
The reference for the element with the ID w3link would be placed inside the JavaScript
variable a1.
All HTML elements support id attributes, which makes them all retrievable by JavaScript. In
this example, all the p elements get IDs, thus making them retrievable using the getElementById() method, too. Take a look at this code:
Hello World
Here's some text.
Here's more text.
Link to the W3
You can retrieve the elements in the same way:
var p1 = document.getElementById("sometext");
var p2 = document.getElementById("moretext");
var plink = document.getElementById("linkp");
But what can you do with those elements after you retrieve them? For elements such as a,
you can access their attributes by retrieving the value of the href attribute, as in this example.
You can find this in the companion code in the getelement.htm file.
Get By Id
Here's some text.
Here's more text.
Link to the W3
The page containing this code displays a dialog box showing the href attribute from the
a element, like the one in Figure 10-2.
208
Part III Integrating JavaScript into Design
Figure 10-2 The href attribute retrieved with the help of getElementById().
Later in this chapter, you see how to change elements and attributes.
A Note on the innerHTML Property
One way to change the text of elements is to use the innerHTML property. The inner
HTML property enables fast and simple access to the text in such elements as a p
element. This property generally works well—so well, in fact, that although it wasn’t
well liked in many web programming circles for some time, I find it difficult to skip it
entirely in this book. So I won’t.
The problem with innerHTML is that it wasn’t formally defined as a standard by the
W3C, so it’s not necessarily supported in all browsers in the way that other DOMspecified objects are. However, innerHTML will be a part of the HTML 5.0 specification,
and with the sometimes unpredictable implementations of the actual DOM specification, innerHTML is still desirable. The major browsers support innerHTML—and they do
so fairly consistently.
Take a look at this example, which you can find in the companion code in the inner
html.htm file:
"http://www.w3.org/TR/html4/strict.dtd">
Get By Id
Here's some text.
Here's more text.
Link to the W3
Chapter 10 The Document Object Model
The changetext() function retrieves the element with an ID of sometext, and places a
reference to it in the variable p1, using this code:
var p1 = document.getElementById("sometext");
Next, it calls the innerHTML property, sending the result to an alert() dialog box.
Notice not only the alert() dialog box, but also the text of the first line in the background window: When the user clicks OK, the alert() dialog box disappears, and the
next line of JavaScript executes, using the innerHTML property to change the text of
the first p element to “Changed Text”. The result is shown here:
209
210
Part III Integrating JavaScript into Design
Retrieving by Tag Name
The getElementById() method works well when you’re retrieving only one or just a few elements, but when you need to retrieve more than one element at a time, you might find the
getElementsByTagName() method to be more appropriate.
The getElementsByTagName() method returns all the elements of the specified tag type in an
array or in a list format. For example, to retrieve all the images (
tags) in a document,
you write the following code:
var images = document.getElementsByTagName("img");
You could then examine the properties of the img elements stored in the images variable by
looping through them.
Here’s an example that modifies a table. This code changes the background color of each td
element within the table when the user clicks the Click To Change Colors link. You can find
this code in the companion content, in the file getbytagname.htm:
"http://www.w3.org/TR/html4/strict.dtd">
Tag Name
Figure 10-3 shows how this page looks when viewed in a web browser.
Figure 10-3 Using getElementsByTagName() to format elements from a table.
Clicking the link causes the table elements to change background color, which you can see in
Figure 10-4.
Figure 10-4 After a user clicks the link, the table elements change the background color.
Examining the code, you see that the JavaScript in the portion of the page creates a
That function retrieves all the td elements by using the getElementsByTagName() method,
The code then enumerates this array using a for loop, starting at element 0, and continuing
to the end of the array. It uses the a1Length variable, which obtained the length of the a1
array in the line preceding the for loop.
Within the for loop, one line of code changes the background style of each element to
#aaabba, a shade of blue. It’s normally better to change the actual style by applying it
through CSS (Cascading Style Sheets) than to explicitly change an attribute, as shown in the
example. However, until you read about CSS and JavaScript in Chapter 15, “JavaScript and
Note The onclick event, along with onload and other events, are covered in detail in Chapter 11.
One common question concerns how to color or shade every other row within a table. You
can do that easily with JavaScript and some CSS, as discussed in Chapter 15.
A number of objects contain groups of elements from a document. These include: