Some Information about the DOM and Client-Side JavaScript
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 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.
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>
document.getElementById("clickMe")
document.myForm.clickMe
(myForm
is the value
of the name
attribute of the form
element)name
and
id
attributes in this context, and the fact that
XHTML elements that are not form controls may no longer even have a
name
attribute.document.forms[0].elements[0]
getElementById()
.background-color
becomes
backgroundColor
document.write()
method can only be
used when initially loading a page. Using it after the fact will
overwrite the entire page.