Document Object Model



index
Disabled back button Next Section
printable version

Section 1: Definition

The document object model (DOM) is a model for describing and accessing each and every node in an HTML or XML document.

The Document Object Model (DOM) is a specification defining what objects should be available to web page scripting.

The DOM allows you to programmatically access and manipulate the contents of a web page (or document).

W3C definition: "The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure, and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page."


Recall that the DOM underlies and is built upon and manipulated by HTML, CSS, JavaScript and XML to separate presentation from content, as seen in the figure below.

Components of a web page

Section 2: Representing Documents as Trees

The W3C DOM, commonly known as 'The DOM' or the 'DOM level 4(or 1/2/3)', provides a more realistic and more versatile way of looking at the structure of HTML documents (as well as XML and other document formats).

HTML elements, the text they contain, and their attributes are all known as nodes.

Section 3: Tree Example

Below you will find some HTML, followed by a graphic representation of the DOM associated with the HTML.

<!DOCTYPE html>
<html lang="en">

<head>
<link href="domExercise.css" rel="stylesheet" type="text/css" />
<title>DOM Exercise</title>
</head>

<body>
<div>
<img class="floatRight" src="../2220logoSm.jpg" alt="2220 logo." style="width:35%;" />
<h1>Practice Exercise: DOM</h1>
</div>
<div style="clear:both;">
<hr/>
<p>Objective: To become familiar with DOM representation.</p>
<p>Using the class notes as a guide, create a tree representation (by hand) of this HTML page.</p>
<hr/>
<a href=../exerciseSolutions/exDOM.htm">Solution</a>
<hr/>
</div>
</body>
</html>

DOM solution
Section 4: Document Interface

Not only does the document object serve as the root of this node tree, it also implements the Document interface, which provides methods for accessing and creating other nodes in the document tree, including:

Note that unlike other nodes, there is only one document object in a page.

Because of the fact that the structure of the DOM tree changes as elements are moved, added, or removed the only reliable way to reference an element is using its id.

<p id="myParagraph">This is a sample paragraph.</p>
.
.
alert(document.getElementById("myParagraph").tagName);

A less direct method to access element nodes is provided by document.getElementsByTagName(), which returns an array of nodes representing all of the elements on a page with the specified HTML tag.

For example, you could change color of every link on a page with the following:

var nodeList = document.getElementsByTagName("a");
for (var i = 0; i < nodeList.length; i++)
nodeList[i].style.color = "#ff0000";

The above code simply updates each link's inline style to set the color to red.


Node Types

As mentioned, there are several types of nodes defined in the document object model, but the ones you'll mostly deal with for web pages are element, text and attribute.

Note that attribute values are always text strings.

Section 5: Walking the DOM

Moving through, or traversing the nodes in the DOM tree structure is often referred to as walking the DOM.

This will be discussed in detail when we reach the section on jQuery methods for traversing the DOM, but if you want more details now, visit any of the following links.

Section 6: Interactivity

Interactivity via Element Attributes (Deprecated) Expand section.


Interactivity via Style Attributes

Most attributes for HTML tags are fairly simple; they define a single value for a property specific to that tag.

Because the style attribute of an element node is defined as an object with properties for every possible style parameter, you can access and update these individual parameters in response to some event.

Here's an example in which the text alignment is defined and altered using a style parameter.

Text in a paragraph element.

Align Left | Align Right

Here is the code:

<p id="styleDemo">Text in a paragraph element.</p>
<a href="" onclick="document.getElementById('styleDemo').style.textAlign = 'left'; return false;">Align Left</a> |
<a href="" onclick="document.getElementById('styleDemo').style.textAlign = 'right'; return false;">Align Right</a>


Interactivity via Dynamic Content

Changing textual content is relatively simple. Every continuous string of character data in the body of an HTML page is represented by a text node.


Text Nodes

Here's another example using a simple paragraph tag – use the links to change the text:

Now look at the code behind it:

<p id="sample1">This is the initial text.</p>

... code for the links

document.getElementById('sample1').firstChild.nodeValue = 'Once upon a time...';
document.getElementById('sample1').firstChild.nodeValue = '...in a galaxy far, far away';

The text nodes do not have an id attribute like element nodes, so they cannot be accessed directly using methods like document.getElementById() or document.getElementsByTagName().

Instead, the code references the text using the parent node, in this case it is the paragraph element with the id "sample1".

This element node has one child node: the text node we want to update, as seen in the diagram below.

Simple Node Tree

So document.getElementById('sample1').firstChild.nodeValue is used to access this text node and read or set its string value.


It is important to remember that text nodes contain just that, text.

For example, using the example above and adding tags make the word "initial" bold:

<p id="sample2">This is the <b>initial</b> text.</p>

... code for the links

document.getElementById('sample1').firstChild.nodeValue = 'Once upon a time...';
document.getElementById('sample1').firstChild.nodeValue = '...in a galaxy far, far away';

...now gives the "sample2" paragraph element three children instead of one.

You can see the structure in the diagram below.

Less Simple Node Tree

The example below demonstrates the code above:

Changing firstChild of the <p> element now only affects the text "This is the ".


Conversely, if you attempt to add markup to the value of a text node, the browser will treat it as plain text.

The changes to the link code seen below:

document.getElementById('sample3').firstChild.nodeValue = '<b>Once</b> upon a time...';
document.getElementById('sample3').firstChild.nodeValue = '...in a galaxy <i>far, far</i> away';

...lead to the following results:

You can avoid problems like this by thinking of text nodes as individual strings of character data located between any two HTML tags; not necessarily matching pairs of tags.

But, but... stick with me!


The innerHTML Property

The recently added innerHTML property represents the character data between an element's starting and ending tag, including other HTML tags.

Using this feature, you could replace the entire contents of the sample paragraph element above, including the HTML markup, using something like:

document.getElementById('sample4').innerHtml = "<b>Once</b> upon a time...";

<p id="sample5">This is the <b>initial</b> text.</p>
<a href="" onclick="document.getElementById('sample5').innerHTML = '<b>Once</b> upon a time...'; return false;">Change Text 1</a>
|
<a href="" onclick="document.getElementById('sample5').innerHTML = '...in a galaxy <i>far, far</i> away'; return false;">Change Text 2</a>

And you can see the effect for yourself:

Section 7: Resources

Resources