Making the Guide Interactive
Introduction: Making the Guide Interactive
Welcome back! In the last lesson, you built a step-by-step cooking guide page using Flask templates and HTML. You set up the structure, added navigation buttons, and connected styles and scripts. That was a great start, but the page is still static — it doesn’t respond to user actions yet.
In this lesson, you will learn how to use JavaScript to make the guide page interactive. By the end, your users will be able to move through recipe steps, hear instructions read aloud, use timers, and rate the recipe — all without reloading the page. This will make your cooking helper much more user-friendly and fun to use.
Quick Recap: The Static Guide Page
Let’s quickly remind ourselves what you built in the previous lesson. You created an HTML page that shows the recipe name, the current step, and navigation buttons like Previous and Next. You also linked a JavaScript file, but so far, it doesn’t do anything.
Fetching and Showing Recipe Data
The first thing we want to do is load the recipe name and steps from the server and display them on the page. We’ll use JavaScript’s fetch function to get the steps from an API endpoint.
Let’s start by waiting for the page to load and then fetching the steps:
Explanation:
- We wait for the page to finish loading using
DOMContentLoaded. - We get references to the elements where we’ll show the recipe name and step text.
- The
loadStepsfunction fetches the steps from the server and updates the page. - If there’s an error, we show a message.
- We will implement the
updateStepfunction later.
Output:
When the page loads, you should see the recipe name and the first step appear.
