Students make elements with createElement and put them on the page with append, loop over an array to build a list, add items from a box, fix a push that never reaches the page, and add features of their own.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 15 min · Make 15 min
Read the unit primer first. This brings together arrays and loops (p5.js) with the page: the array is data, the list on the page is a picture of it. createElement makes an element in memory; append is what shows it. li.remove() takes one away. The classic slip is changing the array and expecting the page to follow, so the bug has no red error. A listener added to each new li is a closure over li: students do not need that word, only that "this li removes itself". Plain append, createElement and remove are enough; we do not use innerHTML on purpose.
Made is not shown: createElement does nothing visible until append. Ask "is it on the page yet?" at every step.
| What you will see | A question to ask | The fix |
|---|---|---|
| Clicking Add changes nothing. | Does the handler make an element and append it? | Make an li, set its text, then list.append(li). |
| Cannot read properties of null (reading append). | Does the list id match the selector? | Match the id exactly, with #. |
| Every item removes at once, or the wrong one. | Is the listener inside the handler, on the new li? | Add li.addEventListener inside the Add handler, before append. |
| Empty items still appear. | Is the check before the li is made? | Put the if and return at the top of the handler. |
One possible solution for each task. Other correct programs are fine.
<input id="item" placeholder="Add an item">
<button id="add">Add</button>
<ul id="list"></ul>
<script>
const input = document.querySelector("#item");
const list = document.querySelector("#list");
document.querySelector("#add").addEventListener("click", function () {
const li = document.createElement("li");
li.textContent = input.value;
li.addEventListener("click", function () {
li.remove();
});
list.append(li);
input.value = "";
});
</script>
<input id="item" placeholder="Add an item">
<button id="add">Add</button>
<ul id="list"></ul>
<script>
const input = document.querySelector("#item");
const list = document.querySelector("#list");
document.querySelector("#add").addEventListener("click", function () {
if (input.value === "") {
return;
}
const li = document.createElement("li");
li.textContent = input.value;
list.append(li);
input.value = "";
});
</script>
<h1>Homework tracker</h1>
<input id="item" placeholder="A task">
<button id="add">Add task</button>
<ul id="list"></ul>
<p><span id="left">0</span> tasks left</p>
<script>
const input = document.querySelector("#item");
const list = document.querySelector("#list");
const left = document.querySelector("#left");
let remaining = 0;
document.querySelector("#add").addEventListener("click", function () {
if (input.value === "") {
return;
}
const li = document.createElement("li");
li.textContent = input.value;
li.addEventListener("click", function () {
li.remove();
remaining = remaining - 1;
left.textContent = remaining;
});
list.append(li);
remaining = remaining + 1;
left.textContent = remaining;
input.value = "";
});
</script>
Stretch: Add a Clear all button, or keep the items in an array as well, and show how many there are.
Support: Give the Add handler complete and ask students only for the remove-on-click.
What is the difference between createElement and append?
createElement makes the element in memory; append puts it on the page, where people can see it.
Next lesson: a page that keeps a score between clicks.