đ§ 1. The Three Main Ways to Change Content
Once youâve selected an element, you can change its content using:
Property | Purpose | Example Output |
---|---|---|
textContent | Change or read only text | âHello Worldâ |
innerHTML | Change or read HTML inside | <b>Hello</b> |
innerText | Change text but respects CSS visibility | âVisible text onlyâ |
1ď¸âŁ textContent
Use when you just want to set or get plain text â not HTML.
<h1 id="title">Old Title</h1>
<script>
const title = document.querySelector("#title");
title.textContent = "New Title";
</script>
â
It replaces only the text.
â
Itâs safe â doesnât parse HTML.
Example:
title.textContent = "<b>Hello</b>";
// Output: <b>Hello</b> (literally as text)
2ď¸âŁ innerHTML
Use when you want to insert HTML elements dynamically.
<div id="content"></div>
<script>
const content = document.querySelector("#content");
content.innerHTML = "<p><strong>Hello</strong> World!</p>";
</script>
â
It parses HTML inside the string.
â ď¸ Be careful when inserting user-generated content, as it can create XSS vulnerabilities.
3ď¸âŁ innerText
Similar to textContent
, but:
- It respects CSS styling (hidden elements wonât be included).
- It triggers layout reflow (slightly slower).
- Often used for visible text.
const para = document.querySelector("p");
console.log(para.innerText); // Text visible on screen only
đ§Š 4. Adding or Appending Content
Instead of replacing, you can append content.
append()
Appends a node or text to the end of an element.
const list = document.querySelector("ul");
const item = document.createElement("li");
item.textContent = "New Item";
list.append(item);
â You can append multiple elements or strings at once:
list.append("End", item);
prepend()
Adds content to the beginning of an element.
list.prepend("Start");
insertAdjacentHTML()
Adds HTML relative to an elementâs position.
element.insertAdjacentHTML(position, text);
Positions:
"beforebegin"
"afterbegin"
"beforeend"
"afterend"
Example:
const div = document.querySelector(".box");
div.insertAdjacentHTML("beforeend", "<p>Added inside!</p>");
âď¸ 5. Removing and Replacing Content
remove()
Deletes the element itself.
document.querySelector(".ad-banner").remove();
replaceChildren()
Clears and replaces children in one go.
const box = document.querySelector(".box");
const newPara = document.createElement("p");
newPara.textContent = "Replaced!";
box.replaceChildren(newPara);
đĄ 6. Example â Dynamic Update
<div class="container">
<h2 class="title">Old Heading</h2>
<button id="change">Change Text</button>
</div>
<script>
const title = document.querySelector(".title");
const button = document.querySelector("#change");
button.addEventListener("click", () => {
title.textContent = "New Heading Updated!";
});
</script>
â Clicking the button instantly changes the heading text.
đ 7. Bonus: Working with Attributes
Sometimes, you want to change content-related attributes like images or links.
<img id="photo" src="old.jpg" alt="Old Image">
<script>
const img = document.querySelector("#photo");
img.setAttribute("src", "new.jpg");
img.alt = "New Image"; // simpler way
</script>
⥠8. Summary
Task | Method | Example |
---|---|---|
Change text | textContent | el.textContent = "New text" |
Change HTML | innerHTML | el.innerHTML = "<b>Hi</b>" |
Add to end | append() | el.append(newEl) |
Add to start | prepend() | el.prepend(newEl) |
Insert HTML | insertAdjacentHTML() | el.insertAdjacentHTML("beforeend", "<p>Text</p>") |
Remove | remove() | el.remove() |
Replace children | replaceChildren() | el.replaceChildren(newEl) |