You can already place content on a page and change how it looks. Today you will build an interactive quiz game for the web with HTML, CSS and JavaScript.
You will set up a CodePen project, show questions and answer buttons, check each choice, keep a score, and finish by writing five quiz questions of your own on a subject you choose.
Before you run anything, look at the first build step and think: what do you expect to see on the page when the starter structure is in place?
Keep this opening tight. Devices on CodePen, students logged in. Optional predict: before anyone builds, ask what they expect to see once the page has a heading and empty quiz, result and score areas. Do not spend long here; time belongs to the build. Pair students if that is your usual routine.
First, open CodePen by visiting https://codepen.io and create a new project.
Model visiting codepen.io and creating a new pen on the board. Check every pair has the HTML, CSS and JS panels visible. Watch for students editing an old pen by mistake. Differentiation: sit with anyone stuck on account or layout so the class moves together into the HTML step.
In this step, we will add the HTML structure for our Interactive Quiz Game. Add the following HTML code to the HTML section of your CodePen:
<h1>Interactive Quiz Game</h1>
<div id='quiz'></div>
<div id='result'></div>
<div id='score'></div>Here's a brief explanation of each element:
<h1>Interactive Quiz Game</h1>: This is the main heading of our quiz game.<div id='quiz'></div>: This div will contain the questions and answer options for our quiz.<div id='result'></div>: This div will display the result of the user's answer, whether it's correct or incorrect.<div id='score'></div>: This div will show the user's score when they complete the quiz.Model pasting the heading and the three empty divs into the HTML panel. Key question: which part of the page will hold the questions, and which will show the result and score? Common bug: a mistyped id will break the JavaScript later, so stress matching ids exactly. No need to style yet.
Now let's add some styling to our HTML elements to make them look better.
Add the following CSS code to the CSS section of your CodePen:
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
padding: 20px;
}
h1 {
text-align: center;
}
#quiz {
background-color: #fff;
padding: 20px;
border-radius: 5px;
}
.answer {
display: block;
margin: 20px auto;
background-color: #007bff;
border: none;
color: #fff;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
}
.answer:hover {
background-color: #0056b3;
}
#result, #score {
text-align: center;
font-size: 24px;
font-weight: bold;
}
Let pairs add the CSS, then run so they see the layout change. Ask what the .answer rule is preparing for, even though no buttons exist yet. Watch for braces left off or a missed selector. Quick win: hover styles will matter once buttons appear.
Before we can use jQuery in our JavaScript, we need to add the jQuery library to the project. In CodePen this is a setting rather than code:
jQuery is a popular JavaScript library that makes it much easier to select parts of the page and change them — you'll recognise it by the $ symbol in the code we write next.
This step is Settings, not typed code. Model: Settings, JS tab, search jquery, add it, Save and Close. Key check: if $ is undefined later, jQuery was not added. Circulate and confirm the library is listed before anyone writes JavaScript.
You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.