Modern websites are rarely static. They respond to clicks, update content instantly, and let users interact without refreshing the page. All of this is made possible through DOM manipulation, which is how JavaScript changes and controls a web page after it has loaded.
Before you can build interactive features, you need to understand how to select elements, update their content, and respond to user actions. This guide walks you through the process step by step, using simple, beginner-friendly examples.
What you’ll learn
By the end of this article, you will:
- Understand how to select elements in the DOM.
- Learn to update text, attributes, and styles dynamically.
- Know how to create, insert, and remove elements.
- Add interactivity with event listeners.
- Follow best practices to keep your code clean and accessible.
Selecting Elements
DOM manipulation always starts with finding the element you want to change.
Example HTML:
<h1 id="title">Hello</h1>
<ul class="items">
<li class="item">A</li>
<li class="item">B</li>
</ul>
JavaScript selectors:
const title = document.getElementById('title'); // select by ID
const firstItem = document.querySelector('.item'); // first match
const allItems = document.querySelectorAll('.item'); // all matches (static list)
Think of selectors as the “address” of your HTML elements. Once you have the address, you can interact with the element directly.
Changing text and HTML
Once selected, you can change what an element shows.
title.textContent = 'Welcome to My Site'; // safe plain text
title.innerHTML = '<em>Dynamic Title</em>'; // allows HTML
- Use
textContent
When updating text only. - Use
innerHTML
only when you need HTML tags.
Avoid setting
innerHTML
With user input, as it can create security risks.
Updating attributes and styles
You can also change element properties like links, images, and inline styles.
const link = document.querySelector('a');
link.setAttribute('href', 'https://example.com');
link.textContent = 'Go to Example';
Working with classes (preferred over inline styles):
const box = document.querySelector('.box');
box.classList.add('active'); // add a class
box.classList.remove('hidden'); // remove a class
box.classList.toggle('open'); // switch on/off
A best practice is to keep visual styling in CSS and just toggle classes with JavaScript.
Creating and removing elements
DOM manipulation also lets you build content on the fly.
const ul = document.querySelector('.items');
const li = document.createElement('li');
li.textContent = 'New item';
li.className = 'item';
ul.appendChild(li); // adds to the end
Removing an element is just as simple:
li.remove();
For larger updates, use a DocumentFragment to add multiple items efficiently.
Adding interactivity with events
The real power of DOM manipulation comes when you respond to user actions.
const btn = document.getElementById('changeBtn');
btn.addEventListener('click', () => {
title.textContent = 'Title updated!';
});
You can also use event delegation to handle many elements with one listener:
const ul = document.querySelector('.items');
ul.addEventListener('click', (e) => {
const li = e.target.closest('.item');
if (li) li.classList.toggle('selected');
});
This approach is efficient and works even if new items are added later.
Mini project: Dynamic To-Do List
Let’s put everything together.
HTML Source Code:
<input id="newText" placeholder="Add item">
<button id="addBtn">Add</button>
<ul id="todo"></ul>
JavaScript Source Code:
const input = document.getElementById('newText');
const addBtn = document.getElementById('addBtn');
const todo = document.getElementById('todo');
addBtn.addEventListener('click', () => {
const text = input.value.trim();
if (!text) return;
const li = document.createElement('li');
li.textContent = text;
li.className = 'item';
todo.appendChild(li);
input.value = ''; // clear input
});
This small app demonstrates selection, content updates, element creation, and event handling within a single flow.
Best practices for beginners
- Use
textContent
for safe text updates and reserveinnerHTML
for trusted content. - Rely on
classList
and CSS for styling rather than inline styles. - Use
data-*
attributes (likedata-js
) for JavaScript hooks instead of styling classes. - Keep code organized: select once, reuse variables, and avoid deeply nested selectors.
- Consider accessibility, update ARIA labels, and manage focus when dynamically changing content.
Conclusion
DOM manipulation is at the heart of making websites dynamic and interactive. By learning how to select elements, update their content, modify attributes, and respond to user actions, you gain the power to transform static pages into living applications. Even simple projects, such as toggling a theme or building a to-do list, help reinforce these concepts and prepare you for more advanced work. With practice, DOM manipulation will feel natural, and you’ll be ready to explore deeper topics like animations, form handling, and building full applications with JavaScript.
You can reach out to me via LinkedIn
Top comments (0)