Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

HTML Tutorial

HTML HOME HTML Introduction HTML Editors HTML Basic HTML Elements HTML Attributes HTML Headings HTML Paragraphs HTML Styles HTML Project I HTML Formatting HTML Quotations HTML Comments HTML Colors HTML CSS HTML Project II HTML Links HTML Images HTML Favicon HTML Page Title HTML Tables HTML Lists HTML Project III HTML Block & Inline HTML Div HTML Classes HTML Id HTML Project IV HTML Global Attributes HTML Buttons HTML Iframes HTML JavaScript HTML File Paths HTML Head HTML Layout HTML Responsive HTML Project V HTML Computercode HTML Semantics HTML Project VI HTML Dialog HTML Details HTML Style Guide HTML Entities HTML Symbols HTML Emojis HTML Character Sets HTML Languages HTML URL Encoding HTML Performance HTML SEO

HTML Forms

HTML Forms HTML Form Attributes HTML Form Elements HTML Input Types HTML Input Attributes Input Form Attributes HTML Form Validation HTML Progress and Meter HTML Project VII

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Video HTML Audio HTML YouTube HTML Project VIII

Web Accessibility

Web Accessibility ARIA Attributes Keyboard Focus and tabindex Landmarks Skip Links Color Contrast

Web APIs

Web APIs Geolocation API Drag and Drop API Popover API Web Storage API Web Workers API Server-Sent Events API

HTML Advanced

HTML and the DOM Web Components HTML Security Advanced Forms

HTML Cert

HTML Certificate

HTML Examples

HTML Examples HTML Videos HTML Editor HTML Quiz HTML Exercises HTML Challenges HTML Projects HTML Website HTML Syllabus HTML Study Plan HTML Interview Prep HTML Summary

HTML References

HTML Tag List HTML Attributes HTML Global Attributes HTML Browser Support HTML Event Attributes HTML Colors HTML Canvas HTML Audio/Video HTML Doctypes HTML Character Sets HTML URL Encode HTML Language Codes HTML Country Codes HTTP Status Codes HTTP Request Methods PX to EM Converter Keyboard Shortcuts

HTML and the DOM


Share

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:

html head body title h1 ul li li My Page Fruits Apple Banana

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 id
  • getElementsByTagName() finds all elements with a tag name
  • innerHTML reads or changes the content of an element
  • JavaScript changes the DOM, not the HTML file

Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.