Understand the web. Build it yourself.

All six exercises / Exercise 04

Find the bug

The script that finds nothing

Match a selector to the element that actually exists.

Observe the evidence

The HTML contains <button id="show-hint">Show hint</button>. The script runs after that markup.

Starting code

HTML / JavaScript · EXAMPLE
const button = document.querySelector("#showHint");
button.addEventListener("click", showHint);
Give me a hint

The selector is an exact identifier, not a description of the button.

Read the solution and corrected code
HTML / JavaScript · EXAMPLE
const button = document.querySelector("#show-hint");
button.addEventListener("click", showHint);

querySelector returns null when no element matches. The next line then tries to call a method on null. The exercise reports that failure without throwing an uncaught error into the lexicon. In your own page, inspect the console and verify the loading order as a separate check.

Make the lesson your own

Replace an id with a class in a local example. Change the selector prefix from # to . and explain the difference.

Read the related guide →

Original exercise by Mario V.W.B.R. Obst with AI assistance. Technical reference: MDN documentation (new tab). Checked September 2026.

Next exercise →