⬅ Previous Topic
Spread and Rest Operators in JavaScriptNext Topic ⮕
JavaScript Selecting Elements in DOM⬅ Previous Topic
Spread and Rest Operators in JavaScriptNext Topic ⮕
JavaScript Selecting Elements in DOMThe DOM (Document Object Model) Tree is a structured representation of an HTML document as a tree of objects. When a web page is loaded, the browser parses the HTML and creates a DOM tree — each HTML element becomes a node in this tree.
If you're serious about web development, understanding the DOM is essential. It's your gateway to interact with the content, structure, and even the styling of the page using JavaScript. From dynamic content updates to building entire interactive applications — it all starts here.
Here's a simple HTML structure:
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello DOM</h1>
<p>Welcome to the DOM tree tutorial</p>
</body>
</html>
Now, imagine this as a tree:
document
html
head
title
body
h1
p
Every element on a page can be accessed through the DOM using JavaScript.
const heading = document.querySelector('h1');
console.log(heading.textContent);
Hello DOM
document.querySelector
allows us to select the first matching element. In this case, it's the <h1>
.
Here’s how you can navigate the tree:
const body = document.body;
console.log(body.firstChild); // Might be a text node (whitespace)
console.log(body.children[0]); // h1 element
console.log(body.lastElementChild); // p element
#text
Hello DOM
Welcome to the DOM tree tutorial
parentNode
– Gets the parent of a nodechildNodes
– Gets a NodeList of child nodes (includes text)children
– Gets an HTMLCollection of child elements (excludes text)firstChild / lastChild
nextSibling / previousSibling
You can change content, structure, or even add new nodes:
const para = document.createElement('p');
para.textContent = 'This is added dynamically!';
document.body.appendChild(para);
Hello DOM
Welcome to the DOM tree tutorial
This is added dynamically!
const oldPara = document.querySelector('p');
oldPara.remove();
After execution, the first paragraph will be removed from the DOM structure.
const newHeading = document.createElement('h2');
newHeading.textContent = 'Updated Heading';
const oldHeading = document.querySelector('h1');
document.body.replaceChild(newHeading, oldHeading);
Updated Heading
Welcome to the DOM tree tutorial
const ul = document.createElement('ul');
for (let i = 1; i <= 3; i++) {
const li = document.createElement('li');
li.textContent = 'Item ' + i;
ul.appendChild(li);
}
document.body.appendChild(ul);
- Item 1
- Item 2
- Item 3
The DOM tree also enables event-driven manipulation. Example:
document.querySelector('h2').addEventListener('click', function() {
alert('Heading clicked!');
});
Clicking the heading triggers a JavaScript alert — a simple but powerful use of DOM interaction.
Practice with live HTML files, inspect elements using browser DevTools, and write JavaScript to see the tree come alive. The more you experiment, the more fluent you become in speaking the language of the web.
⬅ Previous Topic
Spread and Rest Operators in JavaScriptNext Topic ⮕
JavaScript Selecting Elements in DOMYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.