JavaScript Simplified · One concept, one day, one project · Day 14 of 32
Changing HTML and Text
Updating a page’s content from JavaScript, and the important difference between plain text and HTML. You build a profile card that JavaScript fills in.
By the end of today you can
- Set plain text safely with
textContent - Insert HTML with
innerHTML, and know when it is risky - Update several elements to build one card
- Build a dynamic profile card
Yesterday you changed one piece of text. Real pages update many parts at once: a card with a name, a job, a description, a status. Today you fill a whole card from JavaScript, and learn the two ways to put content in.
1. textContent: plain text
textContent sets the text inside an element. Anything you give it is shown exactly as text, even if it looks like HTML. That makes it the safe default.
The <b> did not turn into bold; it showed as characters. textContent treats everything as text, which is exactly what you want for user-supplied content.
2. innerHTML: real HTML
innerHTML sets the HTML inside an element. Tags you give it become real elements. This is powerful for building content, and risky for content that came from a user.
Because innerHTML runs any tags you give it, putting text a user typed straight into innerHTML can let them inject unwanted HTML or scripts. Rule of thumb: use textContent for anything a user typed; use innerHTML only for content you control.
Today’s Mini Project
📰 Dynamic Profile Card
A blank card that JavaScript fills in: name, job, description and a status. You update several elements from one script, the way a real profile page loads.
Notice how Week 3 comes back: the data is an object, and a ternary picks the status word. The DOM is where all the earlier layers finally show up on screen.
Add an avatar letter: a circle showing the first letter of the name, set with profile.name[0]. Expected: a circle with "A" for Ada.
Change active to false and reload in your head: what does the status show?
Expected: "Away". The ternary reads the boolean and picks the matching word.
A profile page, a product card, a dashboard tile: all of them are empty HTML that JavaScript fills from data. You just built that pattern, the one behind most pages you use every day.
Checkpoint
What is the difference between textContent and innerHTML?
textContent sets plain text; tags show as characters. innerHTML sets real HTML; tags become elements. Use textContent for user input, innerHTML only for content you control.
Why is innerHTML risky with user input?
Because it runs any tags given to it. If a user’s text goes straight into innerHTML, they could inject unwanted HTML or scripts. textContent avoids this by treating everything as text.
How do you fill several parts of a card from one script?
Select each element by its id and set its textContent, one after another, usually from a single data object.
Tick Before You Move On
- ☐ I can set plain text with textContent
- ☐ I can set HTML with innerHTML and know when it is risky
- ☐ I can fill several elements from one object
- ☐ I built the Dynamic Profile Card
Quick Recap
textContent sets text exactly; innerHTML sets real HTML. Use textContent for user input; innerHTML only for content you control. A card is empty HTML filled by JavaScript from data. Objects and ternaries from Week 3 come back here on the page.
Next: Day 15. You have changed content. Tomorrow you change the look: colours, themes and classes, with JavaScript.