JavaScript & the DOM

Make pages respond to people — read and change the page with JavaScript, handle clicks and events, and wire up real interactivity. Graded by actually clicking your buttons.

1. From static to interactive

HTML structures the page and CSS styles it — but neither can react. JavaScript is the programming language of the browser: it runs logic, responds to the user, and changes the page while it’s open. This is what turns a document into an application.

JavaScript interacts with the page through the DOM (Document Object Model) — a live, in-memory tree of every element. Change the DOM and the page updates instantly.

2. Selecting elements

To change something, first grab it:

const title = document.querySelector("#title");   // by id
const items = document.querySelectorAll(".item");  // all matches (a list)

querySelector takes any CSS selector and returns the first match (or null).

3. Changing the page

Once you have an element, read or set its content and attributes:

title.textContent = "Hello, Learn.GT!";   // set the text (safe)
title.classList.add("active");            // add a CSS class
title.classList.toggle("night");          // add if absent, remove if present
  • textContent — the element’s text. Use this to safely display text.
  • classList — add / remove / toggle CSS classes (the clean way to change styling from JS).

4. Responding to events

The heart of interactivity: run a function when something happens.

const btn = document.querySelector("#greet");
btn.addEventListener("click", () => {
  document.querySelector("#status").textContent = "Hello, Learn.GT!";
});

addEventListener("click", fn) runs fn every time the button is clicked. The same pattern handles "input", "submit", "keydown" and dozens more.

5. Debugging with the console

console.log(...) prints to the browser console — your most-used debugging tool. In the lab below, the console appears right under the preview.

console.log("ready");   // shows up in the console

6. Wire up an interactive panel

Make the buttons actually do things. The grader clicks your buttons and checks what happens.

Interactive panel
PREVIEW

Your tasks

  • ○Log 'ready' to the console when the script runs
  • ○Clicking #greet sets #status to 'Hello, Learn.GT!'
  • ○Clicking #inc once makes #count show '1'
  • ○Clicking #theme adds the 'night' class to <body>

7. What you learned

You just made a page respond — the leap from a document to an application. You selected elements with querySelector, changed them with textContent and classList, and ran your own logic on click events with addEventListener. That event → read/change-the-DOM loop is the core of every interactive web app, from a toggle to a full single-page application.

Next, the modern JavaScript you will use every day: arrow functions, destructuring, array methods and modules.

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for JavaScript & the DOM. What would you like to know?
📝

Knowledge Check

1 of 3

What does document.querySelector('#count') return?

Report a problem

About: /lessons/javascript-and-the-dom