Skip to content

HTML & the DOM

The Course
•
12 min study

Enough HTML to read the lab

You do not need an earlier HTML course. Read this small piece first:

HTML
<h1>Study time</h1>
<p id="message">Waiting for JavaScript…</p>
  • <h1> starts a main heading and </h1> ends it. The text between the tags is its content.
  • <p> and </p> enclose a paragraph.
  • id="message" is an attribute, extra information inside the opening tag. An id identifies one element on this page.
  • <script> and </script> enclose JavaScript in the full file. HTML outside those tags is not JavaScript.

The <!doctype html> line tells the browser this is an HTML document. The outer html tags contain the page; lang="en" identifies English. The meta charset line declares the text encoding, and title names the browser tab. You can keep that setup while editing the heading, paragraph, and script. Later labs also include a style section for CSS; you do not need to edit its appearance rules to finish the JavaScript tasks.

Find an element and update its text

document is a browser-provided object representing the webpage. You do not declare it yourself in a webpage script.

document.querySelector("#message") finds the element with id message. The # is selector syntax for an ID. It returns null if nothing matches. Assigning to the element's textContent changes its displayed text. Treat user input as text, not HTML: textContent displays characters literally instead of interpreting markup.

Our normal console editors run in a Worker and have no document. Use the Webpage lab later on this page for DOM code. Its script is placed after the elements so they exist before the script searches for them. The preview is isolated from the course page; it cannot edit the surrounding site.

Predict before running

First keep the message-building function independent of HTML. What text will it return?

Predict, then run JavaScript
function greeting(name) {
  return `Welcome, ${name}!`;
}

console.log(greeting("Ada"));
Check your prediction
Text
Welcome, Ada!

Trace each statement in order. If your prediction differed, describe the first place your mental model and the result diverged before trying again.

Separate content, appearance, and behavior

HTML describes elements, CSS controls appearance, and JavaScript handles behavior. A selector finds an existing element; it does not create one. A missing selector returns null, so inspect your HTML spelling before calling a method on it. The first browser lab below contains HTML and JavaScript in one downloadable file; later labs also include CSS. Read its HTML first, find the matching selector, then follow the assignment that changes the page.

Separate content, appearance, and behavior JavaScript
const topic = "<strong>Loops</strong>";

console.log(`You chose ${topic}`);
Check your prediction
Text
You chose <strong>Loops</strong>

In the browser use textContent for learner-entered text. It displays angle brackets as text. Assigning untrusted text to innerHTML instead asks the browser to interpret markup.

Your turn

Exercise

HTML & the DOM: practice

Write summaryText(minutes) that returns Planned: 45 minutes when given 45. Test the result before using it in a webpage.

Your working space JavaScript
// Write summaryText and print summaryText(45).
Hint 1: the idea

This task returns a string, not a DOM element.

Hint 2: a first step

Use a template literal containing the minutes parameter.

Show one solution
JavaScript
function summaryText(minutes) {
  return `Planned: ${minutes} minutes`;
}

console.log(summaryText(45));
Text
Planned: 45 minutes

A pure text-building function can run in either editor. After trying the webpage lab below, you will put this function on a real page.

What you should be able to explain

  • Which part of the lab is HTML, CSS, and JavaScript?
  • What does a missing selector return?
  • Why use textContent for a learner’s topic?

Try the webpage

Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.

Press Run webpage, then change "Ada" in the script to your name and run the page again. Next change the heading’s text in the HTML. Notice which edit changes the greeting and which changes the heading.

Run webpage rebuilds the preview from your edited source. It does not save your changes automatically. Use Download HTML to keep a copy, or Reset code to recover the starting example.

Put your function on the page

Exercise

Show the plan on the page

Use your summaryText function in a real webpage. Edit the page below so that, when you press Run webpage, the paragraph reads:

Text
Planned: 45 minutes

The heading should still say "Study time".

Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.

Hint 1: the idea

The page already finds the paragraph and sets its textContent. Only two things change: which function exists inside the <script> tags, and which function's result is assigned to the paragraph.

Hint 2: a first step

Copy your summaryText function from the console exercise and paste it inside the <script> tags, just above const message. Run the page: nothing changes yet, because the last line still calls greeting("Ada"). That last line is the one to change next.

Show one solution

Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.

summaryText replaces greeting, and the last line assigns summaryText(45) to the paragraph's textContent. The selector "#message" still matches the paragraph's id="message"; if you had renamed one but not the other, querySelector would return null and setting textContent would fail with a TypeError, so nothing would appear. Because summaryText only builds text, it works unchanged in both the console editor and the page.

Was this article helpful?

Your response is saved on this device.