JS DOM Methods
In this page:
document.createElement("tag");
parent.appendChild(element);
element.remove();
element.setAttribute("name", value);
getElementById and querySelector
getElementById() finds a single element by its exact id attribute, while querySelector() finds the first element matching any valid CSS selector, offering far more flexibility, like classes, attributes, or nested combinations.
Example: getElementById and querySelector
<p id="intro">Hello</p>
<script>
// Print `document.getElementById("intro")` to the console
console.log(document.getElementById("intro"));
// Print `document.querySelector(".missing, #intro")` to the console
console.log(document.querySelector(".missing, #intro"));
</script>
querySelectorAll
querySelectorAll() returns every element matching a given CSS selector as a NodeList, which can be looped over with forEach or a standard for...of loop to process every matching element.
Example: querySelectorAll
<p class="item">A</p>
<p class="item">B</p>
<script>
// Call `document.querySelectorAll(".item").forEach(el => console.log(el.textContent))`
document.querySelectorAll(".item").forEach(el => console.log(el.textContent));
</script>
createElement and appendChild
createElement() builds a brand new element in memory, which doesn't appear on the page until it's attached somewhere using appendChild(), which adds it as the last child of a chosen parent element.
Example: createElement and appendChild
<div id="container"></div>
<script>
// Declare the constant `p`, set to `document.createElement("p")`
const p = document.createElement("p");
// Assign "New paragraph" to `p.textContent`
p.textContent = "New paragraph";
// Call `document.getElementById("container").appendChild(p)`
document.getElementById("container").appendChild(p);
</script>
removeChild and innerHTML
removeChild() removes a specific child element from its parent, requiring you to call it on the parent itself, while innerHTML lets you read or completely replace an element's inner markup in one step.
Example: removeChild and innerHTML
<div id="box"><span id="child">Remove me</span></div>
<script>
// Declare the constant `box`, set to `document.getElementById("box")`
const box = document.getElementById("box");
// Call `box.removeChild(document.getElementById("child"))`
box.removeChild(document.getElementById("child"));
// Assign "<p>Replaced content</p>" to `box.innerHTML`
box.innerHTML = "<p>Replaced content</p>";
</script>
classList and setAttribute
classList provides methods like add(), remove(), and toggle() for managing an element's CSS classes without manually parsing the class string, and setAttribute() sets any HTML attribute's value directly.
Example: classList and setAttribute
<div id="box" class="hidden"></div>
<script>
// Declare the constant `box`, set to `document.getElementById("box")`
const box = document.getElementById("box");
// Call `box.classList.remove("hidden")`
box.classList.remove("hidden");
// Call `box.classList.add("visible")`
box.classList.add("visible");
// Call `box.setAttribute("data-status", "active")`
box.setAttribute("data-status", "active");
</script>
- Using getElementById with a leading # symbol, unlike querySelector, getElementById expects just the plain id name.
- Forgetting that querySelectorAll returns a static NodeList, not a live collection that automatically updates as the DOM changes.
- Trying to removeChild an element from the wrong parent, removeChild must be called on the actual direct parent of the element being removed.
- getElementById, querySelector, and querySelectorAll each select elements from the DOM, differing in what selectors they accept and how many elements they return.
- createElement builds a new element, and appendChild or removeChild attach or detach it from the DOM tree.
- innerHTML, classList, and setAttribute let you modify an element's content, CSS classes, and attributes directly.
All standard DOM methods covered here are supported identically across every modern browser and are foundational, stable web platform features.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic