By the end of this lesson, learners will be able to:
textContent, innerText, and innerHTMLOnce you’ve selected an element from the DOM, you can update what it shows or how it looks — all without reloading the page.
This is how websites:
With JavaScript, your webpage becomes alive and responsive to users.
.textContent to change the text inside an element:<p id="status">Loading...</p>
let status = document.getElementById("status");
status.textContent = "Finished!";
Use .textContent when:
.innerHTML (When You Need HTML)If you want to insert HTML elements into your content (like bold text or links), use .innerHTML.
<p id="info">Old info</p>
document.getElementById("info").innerHTML = "<strong>Updated info</strong>";
Be careful when inserting user input with .innerHTML — it can expose your site to XSS attacks.
.innerText vs .textContent| Property | Reads hidden elements? | Reads only visible text? | Includes spacing? |
|---|---|---|---|
.textContent | ✅ Yes | ❌ No | ✅ Yes |
.innerText | ❌ No | ✅ Yes | ✅ Yes |
Use .textContent in most cases for consistency and performance.
Every DOM element has a .style object where you can directly modify CSS properties.
<p id="message">Hello!</p>
let msg = document.getElementById("message");
msg.style.color = "blue";
msg.style.fontSize = "20px";
msg.style.backgroundColor = "#f0f0f0";
Use camelCase for multi-word CSS properties:
background-color → backgroundColorfont-size → fontSizeInstead of changing styles directly, you can apply predefined CSS classes using .classList.
<p id="alert" class="hidden">This is an alert</p>
let alertBox = document.getElementById("alert");
alertBox.classList.remove("hidden"); // Show it
alertBox.classList.add("visible"); // Apply a new style
alertBox.classList.toggle("active");
This is cleaner and more scalable than editing .style directly — keep your CSS rules in a stylesheet.
| Feature | JavaScript Property | Example |
|---|---|---|
| Text content | .textContent | element.textContent = "Hi!" |
| HTML content | .innerHTML | element.innerHTML = "<b>Hi</b>" |
| Style | .style.propertyName | element.style.color = "red" |
| CSS classes | .classList.add/remove | element.classList.add("dark") |