Skip to main content

How to Change an Element's Text with textContent in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<element>.textContent = <text>;
<element>.textContent
PartWhat 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:

index.html, a page with a paragraph Loading... with the id status, an empty paragraph with the id code, and a script that sets the status paragraph's textContent to Ready and the code paragraph's textContent to the words Not bold wrapped in strong tags.

index.html
<!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 page in Chromium with two lines: Ready, and the words Not bold with their opening and closing strong tags written out as plain text, in normal weight.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The first paragraph says Ready. The script replaced Loading... before you could see it.
  • The second paragraph shows <strong>Not bold</strong> as text, and nothing is bold. textContent never reads its value as HTML, so the paragraph still has no elements inside it.
  • That's what makes textContent the safe choice for text that comes from people or from an address: whatever it contains, it can only ever appear as text.

Common Mistakes​

MistakeWhat happensDo this instead
Using textContent to add formattingThe tags appear as text, as in the second paragraphCreate elements, or use innerHTML only with HTML you wrote yourself
Setting textContent on an element that has other elements insideThey'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 innerHTMLThe browser reads it as HTML, so a person can inject elements or scriptsUse textContent for any text you didn't write

Troubleshooting​

SymptomLikely causeFix
Tags such as <strong> show on the pageThe text went in through textContentThat's expected. Build the formatting with elements
Cannot set properties of null (setting 'textContent')The element wasn't foundCheck the id or selector, and run the script after the element
Read text includes words you can't seetextContent includes hidden elementsUse 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​