Table of Contents
The HTML DOM can be accessed with JavaScript (and with other programming languages).
In the DOM, all HTML elements are defined as objects.
The programming interface is the properties and methods of each object.
A property is a value that you can get or set (like changing the content of an HTML element).
A method is an action you can do (like add or deleting an HTML element).
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>DOM API</h4>
<p id="demo">Original content</p>
<script>
document.getElementById("demo").innerHTML = "Content changed by getElementById!";
</script>
</body>
</html>

Result View Example
Common methods for selecting HTML elements:
document.getElementById(id) - Get element by iddocument.getElementsByTagName(name) - Get elements by tag namedocument.getElementsByClassName(name) - Get elements by class namedocument.querySelector(selector) - Get first element matching CSS selectordocument.querySelectorAll(selector) - Get all elements matching CSS selector<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Selecting HTML Elements</h4>
<p class="myClass">First paragraph with class="myClass"</p>
<p class="myClass">Second paragraph with class="myClass"</p>
<p id="demo"></p>
<script>
const elements = document.getElementsByClassName("myClass");
document.getElementById("demo").innerHTML =
"Number of elements with class 'myClass': " + elements.length;
</script>
</body>
</html>

Result View Example
The innerHTML property is the easiest way to get or change the content of an element.
Other ways to access content include:
element.innerHTML - Get or set HTML contentelement.textContent - Get or set text contentelement.innerText - Get or set visible text content<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Accessing Element Content</h4>
<p id="intro">Hello <b>World</b>!</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const element = document.getElementById("intro");
document.getElementById("demo1").innerHTML = "innerHTML: " + element.innerHTML;
document.getElementById("demo2").innerHTML = "textContent: " + element.textContent;
</script>
</body>
</html>

Result View Example
You can change HTML attributes using the DOM:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Changing Element Attributes</h4>
<a id="myLink" href="https://www.w3schools.com">Visit W3Schools</a>
<p id="demo"></p>
<script>
const link = document.getElementById("myLink");
document.getElementById("demo").innerHTML = "Original href: " + link.getAttribute("href");
// Change the href attribute
link.setAttribute("href", "https://www.google.com");
document.getElementById("demo").innerHTML += "<br>New href: " + link.getAttribute("href");
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.