The DOM, as it is almost always affectionately called (probably because Document Object Model is too much of a mouthful to keep repeating) is the language-independent API (Application Programming Interface) that JavaScript and other programming languages can use to get access to and manipulate the various parts of a web page.

The DOM as an Object Hierarchy

The DOM provides an "object hierarchy" model of a web page. The best way to get a feeling for this concept is to study a good example. There are surprisingly few good small examples out there on the web that include both markup and a corresponding DOM diagram. One example which is both concise and good, however, may be found here, along with some useful additional discussion.

The JavaScript Execution Environment and the DOM

It is useful to have a sense of the relationship between the JavaScript "execution environment" and the "Document Object Model" (the DOM), so that you will be better able to access the various DOM elements when working in JavaScript.


The window object for a given window on your screen contains:


Examples of how to access DOM elements in JavaScript (relative to the following markup):

<body>
  <form action="" id="myForm" name="myForm">
    <input type="button" value="Click Me" id="clickMe" name="clickMe" />
  </form>
</body>

Additional Notes

  1. See the W3Schools site for the HTML DOM elements available via JavaScript.

Best Practices

  1. The preferred method of accessing a DOM element is getElementById().
  2. Try to write scripts that do not depend on either "browser sniffing" or "platform sniffing".

Potential Gotchas

  1. To access a CSS style with a hyphenated name via JavaScript, convert the hyphenated name to camel notation.
    Example: background-color becomes backgroundColor
  2. Remember that the document.write() method can only be used when initially loading a page. Using it after the fact will overwrite the entire page.