Introduction
The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. JavaScript DOM manipulation allows you to dynamically update and change your web pages without reloading them.
Accessing Elements
To manipulate the DOM, you first need to access the elements you want to change. There are several methods to do this:
1. getElementById()
This method returns the element that has the ID attribute with the specified value.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<p id="demo">Hello World</p>
<script>
// Access the element with id 'demo'
let element = document.getElementById("demo");
console.log(element); // Outputs the paragraph element
</script>
</body>
</html>
2. getElementsByTagName()
This method returns a collection of all elements with the specified tag name.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<script>
// Get all paragraph elements
let paragraphs = document.getElementsByTagName("p");
console.log(paragraphs.length); // Outputs 2
</script>
</body>
</html>
3. getElementsByClassName()
This method returns a collection of all elements with the specified class name.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<div class="container">Div 1</div>
<div class="container">Div 2</div>
<script>
// Get all elements with class 'container'
let containers = document.getElementsByClassName("container");
console.log(containers.length); // Outputs 2
</script>
</body>
</html>
4. querySelector()
This method returns the first element that matches a specified CSS selector(s).
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<div class="container">Div 1</div>
<div class="container">Div 2</div>
<script>
// Get the first element with class 'container'
let container = document.querySelector(".container");
console.log(container); // Outputs the first div
</script>
</body>
</html>
Modifying Content
Once you have access to an element, you can modify its content.
1. innerHTML
The innerHTML
property sets or returns the HTML content inside an element.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<p id="demo">Hello World</p>
<script>
// Change the inner HTML of the element with id 'demo'
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
</body>
</html>
2. textContent
The textContent
property sets or returns the text content of the specified element and its descendants.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<p id="demo">Hello World</p>
<script>
// Change the text content of the element with id 'demo'
document.getElementById("demo").textContent = "Hello JavaScript!";
</script>
</body>
</html>
3. Creating New Elements
You can create new elements using createElement()
, set their properties, and then add them to the DOM.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<script>
// Create a new paragraph element
let para = document.createElement("p");
// Set the text content of the paragraph
para.textContent = "This is a new paragraph.";
// Get the body element
let body = document.body;
// Append the paragraph to the body
body.appendChild(para);
</script>
</body>
</html>
Adding and Removing Elements
You can dynamically add and remove elements from the DOM.
1. appendChild()
The appendChild()
method adds a node to the end of the list of children of a specified parent node.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<ul id="list">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<script>
// Create a new list item
let li = document.createElement("li");
// Set its text content
li.textContent = "Item 3";
// Get the list
let list = document.getElementById("list");
// Append the new item to the list
list.appendChild(li);
</script>
</body>
</html>
2. removeChild()
The removeChild()
method removes a specified child node from the DOM.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<ul id="list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<script>
// Get the list
let list = document.getElementById("list");
// Get the first item
let item = list.firstChild;
// Remove the first item
list.removeChild(item);
</script>
</body>
</html>
3. replaceChild()
The replaceChild()
method replaces a child node with another node.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<p id="oldPara">Old Paragraph</p>
<script>
// Create a new paragraph
let newPara = document.createElement("p");
newPara.textContent = "New Paragraph";
// Get the old paragraph
let oldPara = document.getElementById("oldPara");
// Get the parent of oldPara (which is the body)
let parent = oldPara.parentElement;
// Replace oldPara with newPara
parent.replaceChild(newPara, oldPara);
</script>
</body>
</html>
Events
DOM manipulation is often triggered by user events like clicks, key presses, etc.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<button id="clickMe">Click Me</button>
<p id="output"></p>
<script>
// Get the button
let button = document.getElementById("clickMe");
// Get the output paragraph
let output = document.getElementById("output");
// Add a click event listener
button.addEventListener("click", function() {
output.textContent = "Button clicked!";
});
</script>
</body>
</html>
Frequently Asked Questions
Q1: What is the difference between innerHTML
and textContent
?
innerHTML
sets or returns the HTML content inside an element, including HTML tags.textContent
sets or returns the text content of an element, without any HTML tags.
Q2: Can I manipulate the DOM without JavaScript?
No, DOM manipulation is typically done using JavaScript. However, CSS can dynamically change styles, which is sometimes referred to as “CSSOM” manipulation.
Q3: What is the difference between appendChild()
and insertBefore()
?
appendChild()
adds a node to the end of the list of children.insertBefore()
adds a node before a specified existing child node.
Q4: How do I remove all elements from a parent node?
You can loop through the children and remove each one, or replace the innerHTML of the parent with an empty string.
Q5: What is the difference between removeChild()
and replaceChild()
?
removeChild()
removes a specified child node from the DOM.replaceChild()
replaces a specified child node with another node.
Conclusion
JavaScript DOM manipulation is a powerful tool for creating dynamic and interactive web pages. By understanding how to access, modify, add, and remove elements, you can create web applications that respond to user actions in real-time. Practice these concepts to build more engaging and interactive web experiences.