DOM Manipulation in JavaScript
JavaScript, the language of the web, empowers developers to create dynamic and interactive web pages. One of the key features that enables this interactivity is Document Object Model (DOM) manipulation.
DOM manipulation allows developers to interact with and modify the structure, style, and content of web pages. In this article, we’ll explore the fundamentals of DOM manipulation in JavaScript, breaking down complex concepts into easy-to-understand snippets.
What is the DOM?
Document Object Model (DOM) is a programming interface that represents the structure of a document as a tree of objects. The document can be an HTML or XML document, and the DOM provides a way for scripts to dynamically access and manipulate the content, structure, and style of the document.
Get sunny Gupta’s stories in your inbox
Join Medium for free to get updates from this writer.
Key points about the DOM:
- Tree Structure:The DOM represents the document as a tree structure, where each node in the tree corresponds to an element, attribute, or piece of text in the document.
- Objects: Each element, attribute, and piece of text in the document is represented by an object in the DOM. These objects can be manipulated using JavaScript to change the content, structure, or style of the document dynamically.
- Document Object: The top-level object in the DOM hierarchy is called the “document” object. It represents the entire document and provides methods and properties for interacting with the document.
- Dynamic Interaction:JavaScript can be used to dynamically modify the content and structure of a web page by interacting with the DOM. This allows developers to create interactive and responsive web pages.
Visual representation of a DOM tree:
How to access DOM elements
To manipulate the DOM, we need to access its elements. This is commonly done using the document object, which represents the entire HTML document. Let's look at a simple example:
// Accessing an element by its ID
const headerElement = document.getElementById('header');
// Accessing elements by class name
const paragraphs = document.getElementsByClassName('paragraph');
// Accessing elements by tag name
const images = document.getElementsByTagName('img');How to modify element content
Once we have access to an element, we can modify its content using the innerHTML property:
headerElement.innerHTML = 'New Header Text';How to Create and Modify Elements
In addition to modifying existing elements, JavaScript allows us to create new elements and add them to the DOM.
How to create new elements
The createElement method is used to create a new HTML element. Let's create a new paragraph element and append (add) it to the body of the document:
const newParagraph = document.createElement('p');
// Setting the text content of the new paragraph
newParagraph.textContent = 'This is a new paragraph.';
// Appending the new paragraph to the body of the document
document.body.appendChild(newParagraph);we create a new p (paragraph) element, set its text content, and then append it to the body of the document.
For example, here’s a simple HTML document:
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<h1 id="myHeading">Hello, DOM!</h1>
<p>This is a paragraph.</p>
<script>
// Accessing and modifying the DOM
var heading = document.getElementById('myHeading');
heading.innerHTML = 'Hello, Updated DOM!';
</script>
</body>
</html>In this example, JavaScript is used to get the element with the ID “myHeading” and change its content using the innerHTML property. This is a basic illustration of how the DOM allows dynamic interaction with the content of a web page.
Thank You…






