A cart badge shows "3". After a click it should show "4". How does JavaScript change what the page says, and how do you avoid showing a visitor's input as code?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
Assigning to textContent replaces an element's text, and the preview updates at once.
const title = document.querySelector("#title");
title.textContent = "Welcome back, Ada";
console.log(title.textContent);
03Objective and prerequisites
Learning objective: Read and change element text with textContent, read input values, understand when innerHTML is appropriate, and avoid injecting untrusted HTML.
el.textContent reads all the text inside an element. Assigning to it replaces the content with plain text.
Form fields store what the user typed in .value, which is always a string, even for numbers.
el.innerHTML reads or writes the HTML markup inside an element. Writing it makes the browser parse tags.
Never put untrusted text (anything a user typed or a server returned) into innerHTML. Use textContent, which shows it as plain text.
Writing untrusted strings with innerHTML enables cross-site scripting (XSS), for example through an <img onerror> attribute. textContent is safe by design.
innerText is layout-aware: it skips hidden elements and applies CSS text transforms, and reading it forces a layout. Prefer textContent unless you need what is visible.
Assigning innerHTML or textContent destroys the old child elements and any listeners attached to them.
Convert input values explicitly with Number() and check the result with Number.isNaN before calculating.
05Definitions
textContent
The text inside an element; assigning it replaces the content with plain text.
innerHTML
The HTML markup inside an element; assigning it parses the string as HTML.
value
The current text of a form field, always a string.
XSS
Cross-site scripting: running attacker-supplied code by inserting untrusted HTML into a page.
A comments widget shows names and messages typed by visitors. Every value is written with textContent, so a message containing HTML tags or script attributes appears as harmless text instead of running in other visitors' browsers.
// li.textContent = `${comment.author}: ${comment.message}`;
console.log("Untrusted text goes into textContent");
12Knowledge check
1. Which property safely writes text a visitor typed?
innerHTML
textContent
outerHTML
insertAdjacentHTML
2. With <input id="q" value="2">, what is printed?
5. What happens to elements inside a div when you assign div.textContent = "Hi"?
They stay and Hi is added
They are removed and replaced by the text
Only text nodes change
Nothing happens
6. A server returns a product name. How should it be displayed?
el.innerHTML = name
el.textContent = name
document.write(name)
eval(name)
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Show the greeting: The page shows the greeting; Prints it
Order total: #total shows the total; Uses the inputs
Safe scoreboard: Prints both scores; No HTML was created; Two list items
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-11-the-document-object-model/lesson-053-reading-and-changing-content/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
textContent reads and writes plain text.
Input value is always a string: convert it.
innerHTML parses markup; never use it with untrusted text.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationchanging page content safely
Context: I am learning changing page content safely in JavaScript.
Task: Explain textContent, innerHTML and input value, including why innerHTML with user input is dangerous.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionchanging page content safely drill
Context: I have practised changing page content safely.
Task: Give me six snippets that read and write content and ask what each prints or shows.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingchanging page content safely bug
Context: My page shows 32 instead of 5 when I add two input values.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationchanging page content safely practice
Context: I finished the changing page content safely exercises.
Task: Create three exercises that read inputs and update text safely.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewchanging page content safely review
Context: Here is my code that uses changing page content safely.
Task: Review my DOM output code for XSS risks and missing type conversion.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.