☰ Year plan · Communication Technology

Dynamic pages, a quiz and a game

Week 14 · Chapter 13 AI Programming (2): dynamic web pages, presenting your site, a quiz app, a block-breaker game and reviewing what you built · 3 lessons
1

Pages that react: dynamic web pages

ICT Secondary 1 Term 1, Chapter 13, Lessons 13-5 to 13-12: pp. 190–202
GoalDescribe how a page reacts to a click or typing, and find a simple bug by reading.
New words
dynamic page · a page that changes when the user does somethingevent · something that happens, such as a click or a key presshandler · the code that runs when an event happensdebugging · finding and fixing a mistake in a program
Explanation

A static page looks the same every time. A dynamic page changes when the user acts. An action is called an event, for example a click on a button or a key being pressed. The code that runs for an event is the handler, and we attach it with a line such as button.onclick = () => { ... }. A page that counts clicks keeps the number in a variable, count. Each click runs count = count + 1 and then writes the new value into the page, so after three clicks the page shows 3. To read what the user typed into an input box, use its value: box.value holds the text, so "Hello " + box.value shows Hello Mona if Mona was typed. If the page does not do what you expect, debug: read any error message, read the code line by line, change one thing, and test again. In the buggy example the page stays empty because JavaScript is case-sensitive: textcontent is not the same as textContent, so the assignment creates a new, useless property and the visible text never changes.

Worked examples
JavaScript
1let count = 0;
2const show = document.getElementById("count");
3document.getElementById("plus").onclick = () => {
4  count = count + 1;
5  show.textContent = count;
6};
A click counter. The variable count starts at 0 and each click adds 1 and shows it.
ClickcountText on the page
000
111
222
333
The state after each click of the counter.
JavaScript
1const box = document.getElementById("name");
2document.getElementById("hello").onclick = () => {
3  document.getElementById("out").textContent = "Hello " + box.value;
4};
Reading an input box. If the user types Mona and clicks, the paragraph shows Hello Mona.
JavaScript
1const show = document.getElementById("count");
2let count = 0;
3document.getElementById("plus").onclick = () => {
4  count = count + 1;
5  show.textcontent = count;
6};
A bug. The page stays empty because textcontent has the wrong case. The fix is a single change: textContent.
Class activity

On paper, trace the click counter for five clicks and write the text on the page after each. Then change the handler so that each click adds 2, and write the new table. Finally write the debugging routine in four short steps.

Practice
1
What is an event? Give two examples.
2
The handler adds 2 instead of 1 (count = count + 2). What does the page show after 5 clicks?
3
In the input example, what does the paragraph show if the user types Sara?
4
Why does textcontent not change the text?