Aral Code

The DOM Lesson 23 of 30

Changing HTML, Styles and Classes with JavaScript

Change text, HTML, attributes, styles and CSS classes with JavaScript, and create, add and remove elements with createElement, append and remove.

Finding elements is step one. Step two is changing them. In this lesson you'll change text, attributes, styles and classes, and create brand-new elements from scratch.

Changing text and HTML

el.textContent = "New text";               // plain text (safe)
el.innerHTML = "<strong>Bold</strong>";    // HTML (use only with your own content)

Changing attributes

Example Swapping an image and a link
<img id="pic" src="/img/mango.svg" alt="Mango" width="120">
<p><a id="link" href="https://en.wikipedia.org/wiki/Mango">About mangoes</a></p>
<button onclick="swap()">Show halo-halo instead</button>
<script>
  function swap() {
    const pic = document.getElementById("pic");
    pic.src = "/img/halo-halo.svg";
    pic.alt = "Halo-halo";
    const link = document.getElementById("link");
    link.href = "https://en.wikipedia.org/wiki/Halo-halo";
    link.textContent = "About halo-halo";
  }
</script>

Common attributes can be set directly: img.src, a.href, input.value, input.disabled, el.id. For any attribute, you can also use setAttribute("name", "value") and getAttribute("name").

Changing styles

el.style changes inline CSS. CSS property names with dashes become camelCase:

el.style.color = "white";
el.style.backgroundColor = "#0f5c4d";   // background-color
el.style.fontSize = "20px";             // font-size
el.style.display = "none";              // hide it

Better: changing classes

Setting many styles in JavaScript gets messy. A cleaner way is to write the styles in CSS and just switch a class on and off with classList:

Example Dark mode toggle with classList
<style>
  body { font-family: Arial, sans-serif; transition: background 0.3s, color 0.3s; }
  body.dark { background: #161b1a; color: #e8efec; }
</style>
<h3>Aral Code</h3>
<p>Click the button to switch themes.</p>
<button onclick="document.body.classList.toggle('dark')">🌙 Toggle dark mode</button>
MethodWhat it does
classList.add("x")Add class x
classList.remove("x")Remove class x
classList.toggle("x")Add if missing, remove if present
classList.contains("x")true/false: does it have class x?

Showing and hiding

el.hidden = true;     // hide
el.hidden = false;    // show
el.hidden = !el.hidden;   // toggle

Creating new elements

Use document.createElement, fill it in, then add it to the page with append:

Example Adding students to a list
<input id="name" placeholder="Student name">
<button onclick="addStudent()">Add</button>
<ol id="list"></ol>
<script>
  function addStudent() {
    const input = document.getElementById("name");
    const name = input.value.trim();
    if (!name) return;

    const li = document.createElement("li");
    li.textContent = name + " ";

    const del = document.createElement("button");
    del.textContent = "✕";
    del.onclick = () => li.remove();
    li.append(del);

    document.getElementById("list").append(li);
    input.value = "";
    input.focus();
  }
</script>
  • parent.append(child): add at the end
  • parent.prepend(child): add at the start
  • el.remove(): delete the element
  • el.before(x) / el.after(x): insert next to an element

Building many elements quickly

For lists built from data, a template literal with innerHTML is quick, as long as the data is your own:

Example A product grid from an array
<style>
  .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; font-family: Arial, sans-serif; }
  .item { border: 1px solid #ddd; border-radius: 10px; padding: 10px; text-align: center; }
  .item b { display: block; color: #0f5c4d; }
</style>
<div class="grid" id="grid"></div>
<script>
  const products = [
    { name: "Pandesal", price: 25 },
    { name: "Kape", price: 8 },
    { name: "Itlog", price: 9 },
    { name: "Noodles", price: 15 },
  ];
  document.getElementById("grid").innerHTML = products
    .map((p) => `<div class="item">${p.name}<b>₱${p.price}</b></div>`)
    .join("");
</script>

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How do you write background-color in el.style?
2. What does classList.toggle("dark") do?
3. Which creates a new <li> element?
4. Which removes an element from the page?

By Sir Reyel · Last updated 2026-12-04