</> Code Corner

JavaScript · JS HTML DOM · Lesson 46

DOM Intro

The Document Object Model.

When a page is loaded, the browser creates a Document Object Model (DOM) of the page — a tree of objects. With the DOM, JavaScript can access and change all the elements of an HTML document.

  • Change all the HTML elements, attributes and CSS styles in the page
  • Remove existing HTML elements and attributes
  • Add new HTML elements and attributes
  • React to all existing HTML events, and create new events

Example

<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
  "Title: " + document.title + "<br>Number of elements in body: " + document.body.getElementsByTagName("*").length;
</script>

Keep reading “DOM Intro”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋