How to Change an Element's Text with textContent in JavaScript
element.textContent = 'Ready' replaces everything inside an element with that text, and element.textContent on its own reads the element's text. Whatever you assign is shown as plain text, so <strong> appears as those characters instead of making anything bold.
Syntax
<element>.textContent = <text>;
<element>.textContent
| Part | What it does |
|---|---|
<element> | The element to change or read, for example from document.getElementById. |
<text> | The new text, as a string. It replaces everything that was inside the element, other elements included. |
Reading textContent gives the text of the element and of everything inside it, joined together. MDN's Node: textContent property page covers both uses.
The Example
The example page has two paragraphs. The script changes the first one from Loading... to Ready, and sets the second one to a piece of text that looks like HTML, to show what textContent does with it.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>textContent</title>
</head>
<body>
<p id="status">Loading...</p>
<p id="code"></p>
<script>
document.getElementById('status').textContent = 'Ready';
document.getElementById('code').textContent = '<strong>Not bold</strong>';
</script>
</body>
</html>
document.getElementById finds each paragraph by its id, and textContent sets its text.
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- The first paragraph says
Ready. The script replacedLoading...before you could see it. - The second paragraph shows
<strong>Not bold</strong>as text, and nothing is bold.textContentnever reads its value as HTML, so the paragraph still has no elements inside it. - That's what makes
textContentthe safe choice for text that comes from people or from an address: whatever it contains, it can only ever appear as text.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using textContent to add formatting | The tags appear as text, as in the second paragraph | Create elements, or use innerHTML only with HTML you wrote yourself |
Setting textContent on an element that has other elements inside | They're all removed: a paragraph holding a <strong> had no elements left after textContent = 'Bye' | Set the text on the inner element you want to change |
Putting text from a user into innerHTML | The browser reads it as HTML, so a person can inject elements or scripts | Use textContent for any text you didn't write |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Tags such as <strong> show on the page | The text went in through textContent | That's expected. Build the formatting with elements |
Cannot set properties of null (setting 'textContent') | The element wasn't found | Check the id or selector, and run the script after the element |
| Read text includes words you can't see | textContent includes hidden elements | Use innerText to get only the visible text |
FAQ
What's the difference between textContent and innerText?
For <p>Hi <strong>there</strong><span hidden> secret</span></p>, textContent was Hi there secret, and innerText was Hi there. innerText gives the text as it's shown, so hidden text is left out. textContent gives all of it.
What's the difference between textContent and innerHTML?
innerHTML reads its value as HTML: setting it to <strong>Bold</strong> created a real <strong> element. textContent never creates elements.
Can I add text instead of replacing it?
Yes: element.textContent += ' more' reads the current text, adds to it, and sets the result.
Conclusion
textContent is the simple, safe way to put text into an element or read it back. It replaces whatever was inside, and it shows any HTML in its value as plain text.
Additional Resources
- Node: textContent property, on MDN.
- HTMLElement: innerText property, on MDN.
- Element: innerHTML property, on MDN.
