More on the DOM
Last updated
Last updated
The Document Object Model, or DOM, allows us to access page elements via JavaScript and perform a variety of actions with them. But before we can do anything, we have to understand more about the DOM's structure and how JavaScript interacts with it.
Review the Document Object Model
Identify the DOM hierarchy
Explore how to access the DOM with JavaScript
The DOM is an intermediate representation that is built from a page's HTML and CSS when the page is loaded. The DOM provides a way of manipulating the HTML it represents. The "way of manipulating" things is an Application Programming Interface, or API. The JavaScript API for DOM manipulation is a vast topic and so we're going to introduce it piece by piece.
First, the DOM has a hierarchical structure. It represents the HTML as parent nodes that have children. A
parent that contains several
s is said to be a parent with multiple children or child-nodes. Let's see this hierarchy in action.
The highest level of the DOM tree is the window
object. Think of the window as the browser window. The window
contains the entire DOM document. All components of your HTML file will be accessible from within the window.
The window
object has a large number of properties that return information about the object. Below are a few examples.
The document
object represents any web page loaded in the browser. The document
contains all the nodes that represent the HTML elements of the page. We use the document
object to traverse through the HTML and manipulate elements.
There are many document
properties that allow us to obtain information about the document
programmatically.
Below the document are all the nodes representing the HTML on the page.
We can alter the DOM several different ways:
Add/remove HTML elements in the page.
You can add elements with functions like appendChild
.
You can remove elements with the similarly named removeChild
.
Both of these functions can be called on any node in the DOM tree.
Add/remove/change HTML attributes.
If you have a DOM node called element
, element.attributes
gives you access to its attributes.
You can remove attributes with removeAttribute
.
Add/remove/change CSS styles.
You can modify any DOM node's style
property.
We can select HTML elements in the document with JavaScript and jQuery:
The DOM will become increasingly important as we use JavaScript to manipulate our sites.
We covered the basics of the Document Object Model and outlined its tree structure. We then dived into the different ways we can use JavaScript to modify page elements, including adding/removing/changing HTML elements, HTML attributes and CSS styles, and listening for or creating events. This fundamental understanding of the DOM will be useful as we work more with JavaScript.
View The DOM on Learn.co and start learning to code for free.