← Layout and components

Make it react

Add a few lines of JavaScript, as a recipe, so a button changes the page and a form greets people by name.

Every like button, quiz and game on a website is a page that reacts: code waits for a click, then changes something.

So far your pages show things. With a few lines of JavaScript, a page can react. This lesson uses JavaScript as a recipe: copy the pattern, change the parts, and see what happens. The DOM course explains how every part works.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

page.html
<p id="message">Ready?</p>
<button id="go">Start</button>
<script>
  document.querySelector('#go').addEventListener('click', function () {
    document.querySelector('#message').textContent = 'Go!';
  });
</script>

Read the code. The <script> block is JavaScript. What happens when someone clicks Start?

What happens when someone clicks Start?

RunRun it and compare the result with your prediction.

Run

Click Start. Then change 'Go!' to your own words (keep the quotes) and click again.

page.html
<p id="message">Ready?</p>
<button id="go">Start</button>
<script>
  document.querySelector('#go').addEventListener('click', function () {
    document.querySelector('#message').textContent = 'Go!';
  });
</script>
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

page.html
<p id="message">Ready?</p>
<button id="go">Start</button>
<script>
  document.querySelector('#go').addEventListener('click', function () {
    document.querySelector('#message').textContent = 'Go!';
  });
</script>

The recipe, piece by piece.

# picks an id, as . picks a class.

Clicking Start should change the message.

.style changes an element's CSS.

<script>
  document.querySelector('#go').addEventListener('click', function () {
    document.querySelector('#message').textContent = 'Go!';
    document.querySelector('#message').style.color = 'green';
  });
</script>

Match each part of the recipe with its job.

document.querySelector('#go')
.addEventListener('click', ...)
function () { ... }
.textContent = 'Go!'

In document.querySelector('#message'), what does '#message' pick?

Clicking Start does nothing, and nothing on the page says why. Click the line that causes it.

What does this extra line do when the button is clicked? document.querySelector('#message').style.color = 'green';

ModifyChange the code on purpose and check the result.

Modify

Change the message to your own words.

Need a hint?

Change only the words between the quotes after textContent =.

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.

Make the message turn a colour when Start is clicked.

Need a hint?

The colour should change when the button is clicked, so the new line goes inside the function, beside the text change.

Another hint

Add a line inside the function, under the textContent line: document.querySelector('#message').style.color = 'green';

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.

Add a Reset button that puts the message back to Ready? (and its colour back to black).

Need a hint?

Reset needs two things: a second button with its own id, and a second recipe that listens to it.

Another hint

Add <button id="reset">Reset</button> after the Start button.

Another hint

Copy the whole recipe, change #go to #reset, and set the words back to Ready?.

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.
Try it yourselfYour edits are saved in this browser.

Make the message turn a colour too.

    This ticks when your page has all of the above.

    Add a Reset button that puts the message back.

      This ticks when your page has all of the above.

      MakeBuild something new using what you have learned. Expect a few tries; that is normal.

      Make

      Make the page greet people: when someone types their name and clicks Say hello, the message shows Hi, Ada! (with their name). .value reads what was typed, and + joins words together.

      Try it yourselfYour edits are saved in this browser.
      Need a hint?

      Start from the recipe: find #hello, wait for a click.

      Another hint

      Inside: const name = document.querySelector('#name').value; reads the box. Then set the message's textContent to 'Hi, ' + name + '!'.

      When Say hello is clicked, show Hi, and the name typed in the box.

        This ticks when your page has all of the above.

        ModifyChange the code on purpose and check the result.

        Make the sign-up say thank you

        This sign-up box is not connected to anything. Add the recipe so that clicking Join shows Thanks, Ada! See you on Wednesday. (with the name that was typed) in the empty paragraph. In a real club, the next step would be sending the name to the club, which needs a program on a server.

        Try it yourselfYour edits are saved in this browser.
        For teachers

        An optional lesson. Students use a short JavaScript recipe (find an element by id, wait for a click, change the page) to make a button change words and colours, and a form greet people by name. About 50 minutes.

        Open the teacher guide: answers, solutions, slides and a worksheet.

        Students will be able to

        Before class

        This lesson is optional and treats JavaScript as a recipe: students copy the pattern, change the parts and see the effect, without needing to understand every symbol. That is deliberate (the same way from turtle import * is a recipe in the Python course). The DOM course explains it properly: what document is, why function () { ... } is passed to addEventListener, and how to write it from scratch.

        The recipe has three parts: find an element (document.querySelector('#go'), where # means id, as . means class in CSS), wait for a click (.addEventListener('click', ...)), change something (.textContent = '...' for words, .style.color = '...' for colour). Do the Make task yourself first.

        Running the lesson

        1. Predict: most students say the words change; a few expect nothing because there is no form. Click to see.
        2. Investigate: name the three parts on the board (find, wait, change). The selectors are the CSS ones they know, plus # for id.
        3. The button that does nothing: `#Go` versus `id="go"`. Like CSS, a mismatched selector fails silently. (The browser's developer console shows an error, if you want to show it.)
        4. Modify: each task changes one part of the recipe. The reset button is a second copy of the whole recipe.
        5. Make: `.value` reads a box and `+` joins words. Students who finish can greet differently by time of day, or count clicks.
        6. Sign-up: the Forms lesson's form, now reacting. Point out what is still missing: sending the name somewhere needs a server.

        The hard part (on purpose)

        The recipe has unfamiliar punctuation, so small slips break it silently. Ask them to compare the id in querySelector with the button's id, quotes and brackets included.

        When students get stuck

        Stretch and support

        Stretch: Count clicks: let count = 0; outside the function, then count = count + 1; and show the count inside it. Or change the page's background: document.body.style.backgroundColor = 'gold';

        Support: Do the first Modify task together, then let students copy and change the recipe for the reset button with the hint open.

        Where it leads

        The DOM course teaches JavaScript properly: variables, decisions, functions and events, with the same querySelector and addEventListener. Students from the Python course will recognise events (onscreenclick there, addEventListener here). Forms that send data somewhere need a server; Gradio (in the Python pathway) is one way to put a program behind a form.

        What each step is for

        Now you can
        You reached the endBack to Layout and components