DOM Tamer — JavaScript

Command the DOM: 15 levels of querySelector, createElement, insertBefore and mutation. A pro-level JavaScript DOM manipulation game.

What you learn in this game

The DOM (Document Object Model) is the representation of an HTML page as JavaScript objects. You can access an element with document.getElementById() then change its properties.

document.querySelector() finds the first element matching a CSS selector. More flexible than getElementById because it can use class, tag, attribute selectors, etc.

Every DOM element has a .style property to change inline CSS. CSS property names use camelCase in JavaScript (background-color → backgroundColor).

.innerHTML lets you insert an HTML string into an element. Different from .textContent, which only inserts plain text (without parsing HTML).

.classList provides methods to manage an element's CSS classes: .add(), .remove(), .toggle(), .contains().

.setAttribute(name, value) sets an HTML attribute on an element. For standard attributes, you can also access them directly as a property (el.href, el.src, etc).

What the levels ask

  1. Change the text inside the element with id "judul" to "Halo Dunia!".
  2. Change the text of the first paragraph (class "intro") to "Selamat datang!".
  3. Change the text color of the #pesan element to "red".
  4. Fill the #container element with the tag <strong>Tebal!</strong>.
  5. Add the class "aktif" to the "#tombol" element.
  6. Add the attribute href="https://belajarweb.dev" to the <a> element with id "link".
  7. Create a new <li> element with the text "Item Baru" and add it into the <ul id="daftar">.
  8. Remove the element with id "hapus-aku" from the DOM.
  9. Make a #btn button that, when clicked, changes the #output text to "Diklik!".
  10. Change the text of all elements with the class "item" to "Sudah diubah".
  11. Toggle the class "dark" on the <body> element. If it's there, remove it. If not, add it.
  12. Get the value from the #nama input and show it in #hasil with the format "Halo, {value}!".

And more, 15 levels in all.