Finding elements is only the first half. The real power of the DOM is that a script can change the text, the attributes, the styles and the structure of a page.
Changing Text and HTML
The property textContent reads or sets the plain text of an element. The property innerHTML reads or sets the HTML inside an element, so tags in the value are turned into elements. Use textContent whenever the value comes from a user. If text typed by a user is placed in innerHTML, a user can insert a script and attack the page.
Definition: DOM manipulation. Changing the content, attributes, styles or structure of a page through the DOM.
Attributes and Styles
The methods getAttribute and setAttribute read and write attributes such as src or href. The style property changes one style directly, for example box.style.color = "red". For many style changes, switching a class is better, as the next unit explains.
Adding and Removing Elements
The method document.createElement("li") makes a new element in memory. The new element is empty and not yet on the page. Its text is set, and then appendChild places it inside a parent. The method remove() deletes an element, together with everything inside it.
Table 3.8: Common DOM changes
| Task | Code | Note |
|---|---|---|
| Change text | el.textContent = "Hi" |
Safe for user text |
| Change HTML | el.innerHTML = "<b>Hi</b>" |
Own markup only |
| Set attribute | img.setAttribute("src", "b.png") |
Any attribute |
| Add element | parent.appendChild(child) |
After createElement |
| Remove element | el.remove() |
Deletes the element |
Example 3.8: A to-do list
<ul id="list"><li>Buy milk</li></ul>
<button id="add">Add item</button>
<script>
const list = document.getElementById("list");
function addItem(text) {
const li = document.createElement("li");
li.textContent = text;
list.appendChild(li);
}
addItem("Call the bank");
addItem("Finish notes");
</script>
Output in the browser:
The function builds each new item and adds it below the existing one. Calling it twice adds two items.
Changing Many Things Safely
When a script adds many elements, it is quicker to build them first and add them together than to change the page again for each one. A loop can create the items, and a single appendChild of a container then places them all. Removing an element is also a good moment to update any count or message that depends on it, so that the page stays consistent. Always test with an empty list and a long list.
Common mistakes
- Putting text typed by a user into innerHTML, which invites an attack.
- Creating an element and forgetting to append it, so that it never appears.
- Setting several styles one by one when changing a class would be simpler.
- Removing an element and then still using the variable that points to it.
Key takeaways
- textContent changes text, and innerHTML changes HTML.
- createElement and appendChild add new elements, and remove deletes them.
- setAttribute changes attributes, and style changes one style directly.
- Use textContent for anything that came from a user.
Practice questions
- Why is innerHTML unsafe for text typed by a user?
- Which two steps put a new element on the page?
- Which property sets only the plain text of an element?
Answers to the practice questions
- The user could insert a script or other markup that the page would then run.
- createElement to make it and appendChild to place it in a parent.
- textContent.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III
