Changing what’s on the page
Once you’ve selected an element, you can change anything about it — its text, HTML, styles, classes, and attributes. This is how vanilla JavaScript makes pages dynamic.Changing text content
textContent — plain text
textContent sets or gets the plain text of an element. It’s safe — any HTML in the string is displayed as text, not rendered.
innerHTML — HTML content
innerHTML parses and renders HTML. Use it when you need to insert structured content.
When to use each
Changing styles
Inline styles
Removing an inline style
element.style only reads and sets inline styles. It won’t show styles from your CSS files. To read the computed (final) style, use getComputedStyle(element).Adding and removing classes
This is the preferred way to change styles. Instead of setting individual CSS properties, toggle classes that are defined in your stylesheet.classList API
Practical examples
Changing attributes
setAttribute / getAttribute
Direct property access
Most common attributes have matching properties:data attributes
Customdata-* attributes are accessed through the dataset property:
data-* attributes are always strings. If you store a number, you’ll need to convert it: Number(card.dataset.userId).Showing and hiding elements
Three common patterns:What’s next?
You can modify existing elements. Now let’s learn how to create new elements from scratch and add them to the page.Creating elements
Dynamically add new HTML elements to the page