Appearance
Introduction to the DOM
What is the DOM
The Document Object Model is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects. That way, programming languages can connect to the page.
A Web page is a document. This document can be either displayed in the browser window or as the HTML source. But it is the same document in both cases. The Document Object Model represents that same document so it can be manipulated. The DOM is an object-oriented representation of the web page, which can be modified with a scripting language such as JavaScript.
The W3C DOM and WHATWG DOM standards are implemented in most modern browsers. Many browsers extend the standard, so care must be exercised when using them on the web where documents may be accessed by various browsers with different DOMs.
DOM and JavaScript
The DOM is not a programming language, but without it, the JavaScript language wouldn't have any model or notion of web pages, HTML documents, XML documents, and their component parts. Every element is a document--the document as a whole, the head, tables within the document, table headers, text within the table cells--is part of the document object model for that document, so they can all be accessed and manipulated using the DOM and a scripting language like JavaScript.
In the beginning, JavaScript and the DOM were tightly intertwined, but eventually, they evolved into separate entities. The page content is stored in the DOM and may be accessed and manipulated via JavaScript, so that we may write this approximative equation:
API = DOM + JS
The DOM was designed to be indepent of any particular programming language, making the structural representation of the document available from a single, consistent API. Though we focus exclusively on JavaScript in this reference documentation, implementations of the DOM can be built for any language.
How Do I Access the DOM
You don't have to do anything special to begin using the DOM. Different browsers have different implementations of the DOM, and these implementations exhibit varying degrees of conformance to the actual DOM standard, but every web browser uses some document object model to make web pages accessible via JavaScript.
When you create a script--whether it's inlne in a <script>
element or included in the web page by means of a script loading instruction--you can immediately begin using the API for the document
or window
elements to manipulate the document itself or to get at the children of that document, which are the various elements in the web page.
Important Data Types
Document. When a member returns an object of type document
, this object is the root document
object itself.
Element. element
refers to an element or a node of type element
returned by a member of the DOM API. Rather than saying, for example, that the document.createElement()
method returns an object reference to a node
, we just say that this method returns the element
that has just been created in the DOM. element
objects implement the DOM Element
interface and also the more basic Node
interface, both of which are included together in this reference.
NodeList. A nodeList
is an array of elements, like the kind that is returned by the method document.getElementsByTagName()
. Items in a nodeList
are accessed by index in either of two ways: list.item(1)
, list[1]
Attribute . When an attribute
is returned by a member, it is an object reference that exposes a special interface for attributes. Attributes are nodes in the DOM just like elements are, though you may rarely use them as such.
NamedNodeMap . A namedNodeMap
is like an array, but the items are accessed by name or index, though this latter case is merely a convenience for enumeration, as they are in no particular order in the list. A namedNodeMap
has an item()
method for this purpose, and you can also add and remove items from a namedNodeMap
.
DOM interface
This guide is about the objects and the actual things you can use to manipulate the DOM hierarchy. There are many points where understanding how these work can be confusing. For example, the object representing the HTML form
element gets its name
property from the HTMLFormElement
interface but its className
property from the HTMLElement
interface. In both cases, the property you want is simply in that form object.
But the relationship between objects and the interfaces that they implement in the DOM can be confusing, and so this section attempts to say a little something about the actual interfaces in the DOM specification and how they are made available.
Many objects borrow from several different interfaces.