Lesson 10 of 28

In progress

Course progress

JavaScript

Changing content, classes and styles

Update text safely, build elements, toggle classes for state, and keep styling decisions in CSS where they belong.

Once you can find an element, the next question is how to change it without breaking the page or opening a security hole. The rule is simple: text goes in as text, structure goes in as elements, and appearance goes in CSS.

What you will learn

  • Set text and attributes safely.
  • Create and insert elements instead of concatenating HTML.
  • Toggle state with classes and custom properties.

Text

HTML
Editable. Runs in your browser.





Press Run to see the output.

textContent reads and writes the text of an element and all its descendants. It cannot interpret markup, which makes it the safe default for anything a user typed.

innerText is similar but depends on rendering: it ignores hidden text and forces layout calculations. Use textContent unless you specifically need the visible text.

HTML, carefully

innerHTML parses a string as HTML. That is useful for trusted templates and dangerous for user input, because a script tag or an event handler in the string can execute.

HTML
Editable. Runs in your browser.





Press Run to see the output.

The first assignment renders bold text. The second shows the literal characters, which is what you want for a comment or a name. If you ever find yourself building HTML with + and a variable, stop and use elements instead.

Creating elements

HTML
Editable. Runs in your browser.





Press Run to see the output.

createElement builds a node, and append inserts it. append accepts several nodes and strings; appendChild takes one node. prepend inserts at the start, and insertBefore places a node relative to another.

Building a list this way is safe and lets you attach listeners to each item. For repeated complex markup, a <template> element plus cloneNode(true) is cleaner than creating ten nested elements by hand.

A template element is the clean way to repeat complex markup. Its contents are inert until cloned, so no scripts run and no images load early.

HTML
Editable. Runs in your browser.





Press Run to see the output.

The clone is a document fragment, so appending it inserts its children. Text still goes in through textContent, which keeps the safety rule intact even when the structure comes from HTML.

Classes for state

HTML
Editable. Runs in your browser.





Press Run to see the output.

classList has add, remove, toggle and contains. The is- prefix is a convention that marks a state class, which makes it obvious in the HTML that the class is managed by script rather than by layout.

Do not set element.style for anything that belongs in a stylesheet. Inline styles are hard to override, they bypass media queries and dark mode, and they scatter design decisions through JavaScript.

Custom properties from script

The one styling job that belongs in JavaScript is a value only the script knows, such as a progress percentage.

HTML
Editable. Runs in your browser.





Press Run to see the output.

Setting a custom property keeps the value inspectable and lets CSS decide how to use it. Direct style.width is fine for a one-off; the token approach scales better when several rules depend on the same number.

Attributes

HTML
Editable. Runs in your browser.





Press Run to see the output.

Use setAttribute for attributes such as aria-label, href and disabled. For booleans, the property form is clearer: button.disabled = true.

When changing href from user data, validate the scheme first. A javascript: URL is executable, and setting it from untrusted input is a script injection.

Building markup with innerHTML and string concatenation turns any user-provided name into potential HTML. A comment containing <img onerror=...> becomes executable. Create nodes and set textContent; it costs three lines and removes the class of bug entirely.

Build a status badge: given a state of "open", "closed" or "pending", set the badge’s text and toggle a matching class. Use the state as a data-state attribute and style it with CSS attribute selectors.

Show solution
HTML
Editable. Runs in your browser.





Press Run to see the output.

The state lives in one attribute, CSS decides the colours, and the text is derived from the same value. Adding a fourth state is a CSS rule plus a call, not another branch in the script.

Recap

  • textContent is the safe way to write text; innerHTML is for trusted markup only.
  • Create elements and append them instead of concatenating HTML strings.
  • State belongs in classes or data attributes, with CSS owning the appearance.

Next, you will respond to clicks, typing and other events.

Does innerHTML remove event listeners?
Replacing the contents destroys the old nodes, and their listeners go with them. Rebuilding is sometimes right; usually you want to update the specific node instead.
Why does my style not apply?
An inline style set earlier may be overriding the stylesheet, or a more specific rule may win. Check the computed styles rather than guessing.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.