Manipulating documents using DOM
The DOM (Document Object Model) is the browser's live, tree-shaped representation of your HTML page, and JavaScript can change it at any time. Manipulating the DOM means selecting elements, changing their text, attributes and styles, and adding or removing elements, so the page updates without reloading. Every interactive feature on a plain JavaScript site relies on it.
Changing content safely
There are two main ways to set an element's content. textContent treats the value as plain text, so any tags inside it are shown as characters rather than parsed. innerHTML parses the string as HTML and builds new elements from it. Use textContent for anything that came from a user or an API, because putting untrusted text into innerHTML can let an attacker inject scripts. Reserve innerHTML for markup you wrote yourself.
Attributes, classes and styles
setAttribute,getAttributeandremoveAttributework with any attribute; many also have direct properties such asimg.srcorinput.disabled.classList.add,remove,toggleandcontainsmanage CSS classes without disturbing others.element.style.backgroundColor = "gold"sets an inline style, using camelCase names.datasetreads and writesdata-*attributes, for examplecard.dataset.id.
Changing classes is usually better than setting many inline styles: keep the look in CSS and let JavaScript just switch classes on and off.
Creating, inserting and removing elements
const list = document.querySelector("#todo-list");
function addTodo(text) {
const item = document.createElement("li");
item.textContent = text; // safe for user input
item.classList.add("todo");
item.dataset.done = "false";
const removeBtn = document.createElement("button");
removeBtn.textContent = "Remove";
removeBtn.type = "button";
removeBtn.addEventListener("click", () => item.remove());
item.append(" ", removeBtn);
list.append(item);
}
addTodo("Buy milk");
addTodo("Revise DOM methods");
// Insert at the top instead
list.prepend(document.createElement("hr"));
// Toggle a highlight on every item
document.querySelectorAll(".todo").forEach((li) => {
li.classList.toggle("highlight");
});
append and prepend add children at the end or start and accept both nodes and strings. before and after insert siblings, replaceWith swaps an element, and remove deletes it. Older code uses appendChild, insertBefore and removeChild, which still work.
Performance and the reflow trap
Each change to the visible page can force the browser to recalculate layout. Adding a hundred items one by one, while also reading sizes such as offsetHeight in between, can make a page stutter. Build the new nodes first, or collect them in a DocumentFragment, then insert them in one go. Avoid alternating reads of layout values with writes inside a loop.
Another trap is the innerHTML += pattern. It looks convenient, but it re-parses the entire content of the element each time, destroying existing child nodes and the event listeners attached to them. Buttons that worked a moment ago suddenly do nothing. Use append or insertAdjacentHTML instead.
Live versus static collections
querySelectorAll returns a static NodeList: a snapshot that does not change when the page does. getElementsByClassName returns a live HTMLCollection that updates automatically, which can cause infinite loops or skipped items if you add or remove matching elements while looping over it.
To loop over a live collection safely, first convert it to a fixed array with Array.from(collection) or the spread syntax.
Takeaway: use textContent for untrusted text, toggle classes instead of writing styles, build elements with createElement and insert them with append, and batch changes to keep pages smooth.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.