HTML and the DOM
What is the DOM?
When the browser loads a page, it reads your HTML and builds a model of the page.
This model is called the DOM (Document Object Model).
The DOM is not a copy of your HTML code - It is a structured, programmable representation of the document.
JavaScript interacts with the DOM to find, update, remove, or add elements, attributes, or text on the page.
The DOM is live, and can change (with Javascript), while your HTML code stays the same.
What is the DOM Tree?
The DOM looks like a family tree.
The <html> element is at the top, and all other elements are inside it.
Look at this small page:
Example
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Fruits</h1>
<ul>
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
</html>
The browser builds this DOM tree from the example above:
Each box in the tree is called a node.
The green boxes are element nodes, and the light boxes are text nodes.
The <ul> element is the parent of the two <li> elements.
The two <li> elements are the children of <ul>.
The two <li> elements are siblings, because they have the same parent.
In the same way, <h1> and <ul> are siblings, and <head> and <body> are siblings.
Find an Element with getElementById
Before you can change an element, you must find it.
The easiest way is to give the element an id, and find it with document.getElementById():
Example
<p id="demo">Hello!</p>
<button onclick="changeText()">Change text</button>
<script>
function changeText() {
document.getElementById("demo").innerHTML = "Hello, World!";
}
</script>
Try it Yourself »
In the example above, the paragraph first says "Hello!". Then, when you click the button, it says "Hello, World!".
The getElementById("demo") finds the element with id="demo".
The innerHTML is the content of the element, and you can give it a new value.
Note: An id must be unique, so only one element on the page can have it.
Find Elements with getElementsByTagName
The document.getElementsByTagName() finds all elements with a specific tag name, like all <li> elements.
It gives you a list of elements:
Example
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
<p id="result"></p>
<script>
const items = document.getElementsByTagName("li");
document.getElementById("result").innerHTML =
"Number of li elements: " + items.length + "<br>" +
"The first one is: " + items[0].innerHTML;
</script>
Try it Yourself »
The page shows:
Number of li elements: 3
The first one is: Apple
items.length returns the number of elements in the list.
items[0] returns the first element, because the list starts at 0.
Change All Elements in the List
Use a for loop to change each element in the list:
Example
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
<button onclick="makeRed()">Make all red</button>
<script>
function makeRed() {
const items =document.getElementsByTagName("li");
for (let i = 0; i < items.length; i++) {
items[i].style.color = "red";
}
}
</script>
Try it Yourself »
Add a New Element
JavaScript can also add new elements to the DOM.
The createElement() creates a new element.
The appendChild() adds it to the page:
Example
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
<button onclick="addFruit()">Add a fruit</button>
<script>
function addFruit() {
const item = document.createElement("li");
item.textContent = "Orange";
document.getElementById("fruits").appendChild(item);
}
</script>
Try it Yourself »
Each click adds Orange to the list.
Note: The HTML file will not change! When you reload the page, the list has only two fruits again.
The DOM is Not Equal to Your HTML
If something is missing in your HTML code, the browser adds it to the DOM.
For example, the browser always adds a <tbody> element to a table, even if you did not write one:
Example
<table>
<tr><td>Apple</td><td>Red</td></tr>
<tr><td>Banana</td><td>Yellow</td></tr>
</table>
<p id="result"></p>
<script>
const count = document.getElementsByTagName("tbody").length;
document.getElementById("result").innerHTML = "Number of tbody elements: " + count;
</script>
Try it Yourself »
The page shows:
Number of tbody elements: 1
Tip: In most browsers, you can right-click a page and choose "Inspect" to see the DOM (If you right-click a page and choose "View page source", you will see the HTML source code).
Learn More
To learn more about finding and changing elements, read the JavaScript HTML DOM tutorial.
Summary
- The browser reads your HTML and builds the DOM, a tree of elements
getElementById()finds one element by its idgetElementsByTagName()finds all elements with a tag nameinnerHTMLreads or changes the content of an element- JavaScript changes the DOM, not the HTML file