Introduction To The DOM - Web APIs - MDN
Introduction To The DOM - Web APIs - MDN
All of the properties, methods, and events available for manipulating and creating web
pages are organized into objects. For example, the document object that represents the
document itself, any table objects that implement the HTMLTableElement DOM interface for
accessing HTML tables, and so forth, are all objects.
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 1/11
3/3/24, 7:04 PM Introduction to the DOM - Web APIs | MDN
The DOM is built using multiple APIs that work together. The core DOM defines the entities
describing any document and the objects within it. This is expanded upon as needed by
other APIs that add new features and capabilities to the DOM. For example, the HTML
DOM API adds support for representing HTML documents to the core DOM, and the SVG
API adds support for representing SVG documents.
For more information on what technologies are involved in writing JavaScript on the web,
see JavaScript technologies overview.
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 2/11
Accessing the DOM
3/3/24, 7:04 PM Introduction to the DOM - Web APIs | MDN
You don't have to do anything special to begin using the DOM. You use the API directly in
JavaScript from within what is called a script, a program run by a browser.
When you create a script, whether inline in a <script> element or included in the web
page, you can immediately begin using the API for the document or window objects to
manipulate the document itself, or any of the various elements in the web page (the
descendant elements of the document). Your DOM programming may be something as
simple as the following example, which displays a message on the console by using the
console.log() function:
HTML
As it is generally not recommended to mix the structure of the page (written in HTML) and
manipulation of the DOM (written in JavaScript), the JavaScript parts will be grouped
together here, and separated from the HTML.
For example, the following function creates a new h1 element, adds text to that element,
and then adds it to the tree for the document:
HTML
<html lang="en">
<head>
<script>
// run this function when the document is loaded
window.onload = () => {
// create a couple of elements in an otherwise empty HTML page
const heading = document.createElement("h1");
const headingText = document.createTextNode("Big Head!");
heading.appendChild(headingText);
document.body.appendChild(heading);
};
</script>
</head>
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 3/11
3/3/24, 7:04 PM Introduction to the DOM - Web APIs | MDN
<body></body>
</html>
Note: Because the vast majority of code that uses the DOM revolves around
manipulating HTML documents, it's common to refer to the nodes in the DOM as
elements, although strictly speaking not every node is an element.
Element implement the DOM Element interface and also the more basic Node
interface, both of which are included together in this reference. In an
HTML document, elements are further enhanced by the HTML DOM
API's HTMLElement interface as well as other interfaces describing
capabilities of specific kinds of elements (for instance,
HTMLTableElement for <table> elements).
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 4/11
3/3/24, 7:04 PM Introduction to the DOM - Web APIs | MDN
Attr special (albeit small) interface for attributes. Attributes are nodes in
the DOM just like elements are, though you may rarely use them as
such.
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,
NamedNodeMap 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
DOM interfaces
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 in that form object.
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 5/11
3/3/24, 7:04 PM Introduction to the DOM - Web APIs | MDN
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.
Interfaces and objects
Many objects implement several different interfaces. The table object, for example,
implements a specialized HTMLTableElement interface, which includes such methods as
createCaption and insertRow . But since it's also an HTML element, table implements the
Element interface described in the DOM Element Reference chapter. And finally, since an
HTML element is also, as far as the DOM is concerned, a node in the tree of nodes that
make up the object model for an HTML or XML page, the table object also implements the
more basic Node interface, from which Element derives.
When you get a reference to a table object, as in the following example, you routinely use
all three of these interfaces interchangeably on the object, perhaps without knowing it.
JS
from the generic Node interface, and together these two interfaces provide many of the
methods and properties you use on individual elements. These elements may also have
specific interfaces for dealing with the kind of data those elements hold, as in the table
object example in the previous section.
The following is a brief list of common APIs in web and XML page scripting using the DOM.
document.querySelector()
document.querySelectorAll()
document.createElement()
Element.innerHTML
Element.setAttribute()
Element.getAttribute()
EventTarget.addEventListener()
HTMLElement.style
Node.appendChild()
window.onload
window.scrollTo()
Examples
Setting text content
This example uses a <div> element containing a <textarea> and two <button> elements.
When the user clicks the first button we set some text in the <textarea> . When the user
clicks the second button we clear the text. We use:
Document.querySelector() to access the <textarea> and the button
EventTarget.addEventListener() to listen for button clicks
HTML
HTML Play
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 7/11
3/3/24, 7:04 PM Introduction to the DOM - Web APIs | MDN
<div class="container">
<textarea class="story"></textarea>
<button id="set-text" type="button">Set text content</button>
<button id="clear-text" type="button">Clear text content</button>
</div>
CSS
CSS Play
.container {
display: flex;
gap: 0.5rem;
flex-direction: column;
}
button {
width: 200px;
}
JavaScript
JS Play
Result
Play
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 8/11
3/3/24, 7:04 PM Introduction to the DOM - Web APIs | MDN
HTML
HTML Play
<div class="container">
<div class="parent">parent</div>
<button id="add-child" type="button">Add a child</button>
<button id="remove-child" type="button">Remove child</button>
</div>
CSS
CSS Play
.container {
display: flex;
gap: 0.5rem;
flex-direction: column;
}
button {
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 9/11
3/3/24, 7:04 PM Introduction to the DOM - Web APIs | MDN
width: 100px;
}
div.parent {
border: 1px solid black;
padding: 5px;
width: 100px;
height: 100px;
}
div.child {
border: 1px solid red;
margin: 10px;
padding: 5px;
width: 80px;
height: 60px;
box-sizing: border-box;
}
JavaScript
JS Play
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 10/11
Result
3/3/24, 7:04 PM Introduction to the DOM - Web APIs | MDN
Play
Specifications
Specification
DOM Standard
This page was last modified on Nov 29, 2023 by MDN contributors.
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 11/11