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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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>
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