Changing DOM Content
Table of Contents + −
In the previous lesson, we learned how to select elements on the page. Now let’s change what those elements show, from the text inside them to the attributes that control them.
✏️ Changing Text with textContent
Once you have selected an element, the textContent property lets you read and change the plain text inside it. Assigning a new string replaces whatever text was there before.
const heading = document.querySelector("h1");heading.textContent = "Welcome back!";Here’s what each line does:
- The first line selects the page’s
<h1>element and stores it in theheadingvariable. - The second line assigns a new string to
heading.textContent, replacing the old heading text with “Welcome back!”.
The browser treats the new value as plain text, so any HTML symbols like < or > show up exactly as typed instead of becoming tags. This makes textContent the safe default for putting text on the page.
🧱 Changing Markup with innerHTML
The innerHTML property changes the HTML markup inside an element. The browser parses the string and turns it into real elements.
const box = document.querySelector("#message");box.innerHTML = "<strong>Saved</strong> your changes.";Let’s walk through it:
- The first line selects the element with the id
messageand stores it inbox. - The second line assigns a string containing a
<strong>tag tobox.innerHTML, so the browser parses it and renders “Saved” in bold followed by ” your changes.”
Use innerHTML only when you actually need to insert tags, and only with content you control.
innerHTML can be dangerous
Never set innerHTML from user input or other untrusted data. A malicious
string can inject scripts and attack your visitors. When you only need text,
use textContent instead.
📊 textContent vs innerHTML
Both change what is inside an element, but they treat the value differently.
| Property | Treats value as | Use when | Safe with user input |
textContent | Plain text | You just need to show text | Yes |
innerHTML | HTML markup | You need to insert tags | No |
🔗 Changing Attributes
Elements also have attributes such as src, href, and value. You can change these with setAttribute, or directly through matching properties on the element.
const link = document.querySelector("a");
// ✅ Direct propertylink.href = "https://example.com";
// ✅ setAttribute does the same thinglink.setAttribute("href", "https://example.com");Here’s what each line does:
- The first line selects the page’s
<a>element and stores it inlink. - Setting
link.hrefdirectly updates the link’s destination through its property. - Calling
link.setAttribute("href", ...)changes the samehrefattribute by name, giving the identical result.
Common attribute changes look like this:
const image = document.querySelector("img");image.src = "logo.png"; // swap the picture
const field = document.querySelector("input");field.value = "Alex"; // set the text in an inputLet’s break it down:
- The first two lines select an
<img>and set itssrctologo.png, which swaps the displayed picture. - The last two lines select an
<input>and set itsvalueto “Alex”, which fills the field with that text.
Inputs use value, not textContent
The text a user types into an <input> lives in its value property, not in
textContent. Read and change input text through value.
🖱️ A Practical Example
This example combines selecting an element, listening for a click, and changing content. Clicking the button updates the message text.
const button = document.querySelector("#save");const status = document.querySelector("#status");
button.addEventListener("click", () => { status.textContent = "Your work has been saved.";});Here’s how it works, step by step:
- The first two lines select the button with the id
saveand the status element with the idstatus. button.addEventListener("click", ...)tells the button to run the given function every time it is clicked.- Inside that function,
status.textContentis replaced with the confirmation message “Your work has been saved.”
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
Using innerHTML for plain text | Opens the door to script injection | Use textContent unless you need tags |
| Changing content before selecting | The element is null, so it throws an error | Select the element first, then change it |
Setting textContent on an input | The field stays empty | Set value to change input text |
🔧 Try It Yourself!
- Select a heading and change its text with
textContent. - Select an element and add a bold word inside it with
innerHTML. - Select an
<img>and swap its picture by changingsrc. - Add a button that updates a message element’s text when clicked.
🧩 What You’ve Learned
- ✅
textContentchanges the plain text inside an element and is the safe default - ✅
innerHTMLchanges the HTML markup but is risky with untrusted input - ✅
setAttributeand direct properties likesrcandhrefchange attributes - ✅ Inputs hold their text in
value, nottextContent - ✅ You must select an element before you can change it
🚀 What’s Next?
Now that you can change what an element shows, let’s change how it looks. Let’s continue to Changing Styles.