JavaScript — The basics

Guide 03 / 4

JavaScript workshop · À table !

Let users search for a recipe by its title and duration, display the number of results and reset the filters without reloading the page.

Expected outcome :

Reproduce a working result, test it, then succeed at an exercise without copying.

Reading
0%

01 · Activating the filters

The complete app.js file is at the end of the workshop.

Step 1 · Preparing the files

Keep index.html and style.css, add app.js and its script defer tag in head. Start the server from mini-blog/ and open http://localhost:8080.

python3 -m http.server 8080
Remember :

If Python is not available, use the editor's local server. Expected result: the three recipes and the filter form are visible.

Step 2 · Following a filtering

Type soupe. The input becomes a normalised string. The script goes through the three cards, compares their title, changes hidden then updates the counter.

Saisie : soupe
Attendu : Soupe de carottes uniquement, 1 recette affichée.

Step 3 · Testing the usual cases

Test each scenario below, resetting the filters between attempts. Compare the number and the titles, not just the appearance.

Vide + toutes durées → 3
  SOUPE   + toutes durées → 1
Vide + 20 minutes → soupe et salade, 2
tarte + 20 minutes → 0 et message d’aide
zzzz + toutes durées → 0 et message d’aide
Réinitialiser → 3
Remember :

These results assume the three reference recipes; the optional compote from the HTML course would add one result.

Step 4 · Testing without JavaScript

Temporarily disable the script tag in HTML then reload the page. Then re-enable the script.

Attendu sans script :
- les trois recettes sont visibles ;
- le formulaire est caché ;
- aucun message d’erreur n’empêche de lire.
Remember :

This is progressive enhancement: the content is already useful before the interactions.

02 · Practising without copying

Try before reading the solution.

Exercise · Add a 15-minute choice

Add a 15 minutes option in the HTML menu, between Toutes les durées and 20 minutes. The JavaScript must not change.

Attendu avec les trois recettes de référence : seule Salade de tomates reste visible.

Solution · Let the data do the work

Add this option in select#duree-max. Number and the <= comparison automatically use its value.

<option value="15">15 minutes</option>
Remember :

You change the data offered by the HTML; the same filtering function handles the new case.

Exercise · Remember the last search term, as an extension

Once the filters work, try keeping the searched text in localStorage. This is storage specific to the browser and to the site's origin, not a shared database.

Objectif : recharger la page et retrouver la saisie précédente.
Remember :

Optional extension. Privacy settings may make storage unavailable; keep the filtering usable.

Extension solution · Local storage

In app.js, insert the read before the final first call to filtrerRecettes. Then add the listener below. try/catch stops unavailable storage from breaking the page.

try {
  recherche.value = localStorage.getItem("a-table-recherche") ?? "";
} catch (erreur) { console.info("Stockage indisponible"); }
recherche.addEventListener("input", () => {
  try { localStorage.setItem("a-table-recherche", recherche.value); }
  catch (erreur) { console.info("Stockage indisponible"); }
});
Remember :

This extension remembers the input entry only; the default reset button does not update this saved value. For a persistent reset, remove the key in its listener.