Byte by Byte › Book 8: Real-World Coding, Data and AI › Chapter 4: The DOM and Events: Making Web Pages Interactive
Chapter 4: The DOM and Events: Making Web Pages Interactive

🔎 The Case
Simbha's code works, but visitors cannot open a browser console. The booking logic must connect to the page itself: a box to type tickets into, a button to press, and a message that appears on screen. Beautine, Keeper of Records, looks at the HTML and says, "It's a family tree." She is right, and that tree has a name.
💡 The Concepts
The DOM (Document Object Model). When a browser loads HTML, it builds a tree of objects in memory, one node per element. document is the root. JavaScript reads and changes this tree, and the browser redraws the page instantly. HTML is the starting state; the DOM is the live page.
Selecting elements.
document.getElementById("total")finds the element withid="total".document.querySelector(".stand")finds the first match for any CSS selector;querySelectorAllfinds all matches.
Changing elements.
el.textContent = "Booked!"sets the text.el.style.color = "red"changes one style; better,el.classList.add("full")applies a CSS class.input.valuereads what the user typed (always a string).document.createElement("li")pluslist.appendChild(item)adds new elements.
Events. An event is something that happens: a click, a key press, a form submit, the page finishing loading. An event listener is a function that runs when the event happens:
button.addEventListener("click", handleBooking);
This is event-driven programming: the program sets up listeners, then waits. Common events: click , input , change , submit , keydown , mouseover .
Forms. When a form submits, the browser reloads the page by default. Call event.preventDefault() inside the handler to stop that.
Saving data in the browser. localStorage.setItem("seats", "120") and localStorage.getItem("seats") keep small text values between visits on that one browser. Never store passwords or personal data there.
Security note. Never put text typed by users into the page with innerHTML ; a user could inject a script (this attack is called cross-site scripting, XSS). Use textContent .
🛠️ Build It
Replace the <body> of index.html :
<body>
<h1>Memunic Games Booking</h1>
<form id="bookForm">
<label>Stand
<select id="stand">
<option>North</option><option>South</option>
<option>East</option><option>West</option>
</select>
</label>
<label>Tickets <input id="tickets" type="number" min="1" value="1"></label>
<button type="submit">Book</button> </form> <p id="message"></p> <ul id="standList"></ul> <script src="app.js"></script> </body>
Keep allStands , bookingCost and book from Chapter 3 (delete the console.log lines) and add:
const form = document.getElementById("bookForm");
const message = document.getElementById("message");
const list = document.getElementById("standList");
function showStands() {
list.textContent = "";
for (const s of allStands) {
const li = document.createElement("li");
li.textContent = `${s.name}: ${s.seats > 0 ? s.seats + " seats left" :
"FULL"}`;
if (s.seats === 0) li.classList.add("full");
list.appendChild(li);
}
}
form.addEventListener("submit", function (event) {
event.preventDefault();
const standName = document.getElementById("stand").value;
const tickets = Number(document.getElementById("tickets").value);
message.textContent = book(standName, tickets);
showStands();
});
showStands();
Add <style>.full { color: crimson; text-decoration: line-through; }</ style> in the <head> . Book tickets until a stand turns red.
✅ Case Closed
The booking desk is now a real web app: the page reacts to the user, validates input and updates itself without reloading. On Games morning the queue moves in minutes. Pip asks, "What happens if I refresh?" The seats reset. Memoon notes this honestly as a limitation: real systems store bookings on a server in a database, so everyone sees the same numbers. That is what back-end developers build; what you built is the front end.
❓ Test Yourself
- What is the DOM, and how is it different from the HTML file?
- Write the code to change the text of
<h2 id="title">to "Results". - What does
addEventListenerdo? Name four events. - Why do we call
event.preventDefault()on a form? - Why is
textContentsafer thaninnerHTMLfor user input? - Challenge: Add a "Reset" button that sets every stand back to its starting seats and redraws the list.
📚 Key Terms
DOM · node · document · getElementById · querySelector · textContent · classList · value
- createElement · appendChild · event · event listener · event-driven programming ·
preventDefault · localStorage · cross-site scripting (XSS) · front end · back end · server
Keep learning with Byte by Byte
Explore Book 8: Real-World Coding, Data and AI. Find the ebook, purchase details and teaching slides on MemuniCity.
Comments
Post a Comment