JavaScript HTML DOM

Back to JS page

Table of Contents

HTML Document Object Model

The HTML DOM is a standard object model and programming interface for HTML. It defines:

In other words: The HTML DOM is a standard for how to get, change, add, or delete HTML elements.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript HTML DOM</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>HTML DOM</h4>
<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>

</body>
</html>

Example 1

Result View Example

The DOM Tree

The HTML DOM model is constructed as a tree of objects:

With the object model, JavaScript gets all the power it needs to create dynamic HTML:

Accessing HTML Elements

JavaScript can access HTML elements using different methods:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript HTML DOM</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Accessing HTML Elements</h4>
<p id="intro">Finding HTML Elements by Id</p>
<p id="demo"></p>

<script>
const element = document.getElementById("intro");
document.getElementById("demo").innerHTML =
  "Text from intro paragraph: " + element.innerHTML;
</script>

</body>
</html>

Example 2

Result View Example

Document

Document in project

You can Download PDF file.

Reference