A Friendly Introduction to the Document Object Model (DOM) API

Acronym DOM on wood planks

If you’ve ever made a web page respond to a click, animate a box, or add content without refreshing the page, you’ve used the DOM—whether you realized it or not. The Document Object Model (DOM) is the browser’s structured, in-memory representation of your HTML. It turns your static markup into a tree of objects you can read and manipulate with JavaScript.

At a high level:

Core DOM Operations

Selecting elements

Responding to events

Reading and updating content

Styling and classes

Attributes and data

Creating and inserting nodes

Inspecting the DOM

				
					<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DOM API Demo</title>
    <link href="style.css" rel="stylesheet" />
    <script src="documentObjectModelAPI.js" defer></script>
</head>

<body>
    <h1>DOM API Demo</h1>
    <div id="demo-box">This is a demo box.</div>
    <button id="change-btn">Change Box</button>
    <button id="add-btn">Add Paragraph</button>
</body>

</html>
				
			
				
					#demo-box {
    padding: 1em;
    background: #f0f0f0;
    border: 1px solid #ccc;
    margin-bottom: 1em;
}

.highlight {
    background: #ffe066 !important;
}
				
			
				
					// Wait for the DOM to be fully loaded before running code
document.addEventListener('DOMContentLoaded', () => {
	// Select elements using DOM API
	const box = document.querySelector('#demo-box');
	const changeBtn = document.querySelector('#change-btn');
	const addBtn = document.querySelector('#add-btn');

	// Event handler: Change box content, style, and attribute
	changeBtn.addEventListener('click', () => {
		// Change the text content
		box.textContent = 'The box has been changed!';

		// Toggle a CSS class to change the background
		box.classList.toggle('highlight');

		// Set a custom attribute
		box.setAttribute('data-changed', 'true');
	});

	// Event handler: Add a new paragraph below the box
	addBtn.addEventListener('click', () => {
		// Create a new <p> element
		const newPara = document.createElement('p');
		newPara.textContent = 'This paragraph was added using the DOM API.';

		// Insert the new element after the box
		box.insertAdjacentElement('afterend', newPara);
	});

	// Demonstrate reading attributes and logging to the console
	console.log('Box ID:', box.id); // Accessing a property
	console.log('Box initial content:', box.textContent); // Reading text content
});

				
			

Leading Practices

Why the DOM Matters

The DOM is the bridge between your code and the page your users see. It enables progressive enhancement, dynamic UX, and component-driven interfaces. Whether you’re writing vanilla JavaScript or using a framework, understanding the DOM API gives you the mental model to debug issues, measure performance, and build accessible, maintainable interfaces.

more posts:
Online video streaming platform and digital content management concept with media player interface, cloud library, and multimedia files on laptop, representing content distribution, video marketing
Code

Breaking into the Pipeline: Real-Time Frame Manipulation with the Insertable Streams for MediaStreamTrack API

The experimental Insertable Streams for MediaStreamTrack API closes a long-standing gap in web multimedia by treating live camera feeds as standard Web Streams. By piping individual VideoFrame objects through a TransformStream and recording the transformed output via MediaRecorder, developers can execute frame-by-frame processing—like real-time filtering, watermarking, and computer vision overlays—without relying on hidden canvas hacks.

Discover More »
Apps & Custom Software

What Building Our Own App Taught Us About Building Yours

When Hess WebTech built and launched Tericipe—an Android app that turns printed recipes into a searchable digital collection—in under three months, we had to live with every decision we usually advise clients on. From testing against real-world messy data and handling AI uncertainty to server-side payment security and post-launch maintenance, here are five crucial lessons from our own build that apply to almost any software project.

Discover More »