Development and production

Course Beginner Free

JavaScript — The basics

Make the page interactive: variables, functions, the DOM and events to filter recipes without reloading.

  • 4 guides
  • 38 concepts
Start reading

Guides

  1. 01 Guide JavaScript glossary Explain the concepts in your own words and recognise them in an example. 14 concepts
  2. 02 Guide JavaScript essentials Choose and use the essential elements to complete this step of the blog. 10 concepts
  3. 03 Guide JavaScript workshop · À table ! Reproduce a working result, test it, then succeed at an exercise without copying. 8 concepts
  4. 04 Guide Resources and review Find reliable information and explain what you have built. 6 concepts

Suggested path

  1. 01. HTML: structure three recipes in a semantic page.
  2. 02. CSS: make the blog readable on mobile and on desktop.
  3. 03. JavaScript: filter the recipes without reloading the page.
  4. 04. PHP: produce the pages from arrays and process a form.
  5. 05. MySQL: keep the recipes in a relational database.
  6. 06. PDO: read and add recipes from PHP with prepared queries.

Sources and references

Beginner workshop, step 3/6. The complete files are materials to reproduce in a separate local project. Read the prerequisites, the expected results and the limits stated in the workshop.

Example files

Ajout dans head de index.html
<script src="app.js" defer></script>
app.js
"use strict";

// Le script defer est exécuté après la lecture du HTML.
const formulaire = document.querySelector("#filtres");
const recherche = document.querySelector("#recherche");
const dureeMax = document.querySelector("#duree-max");
const compteur = document.querySelector("#compteur");
const aucun = document.querySelector("#aucun");
const cartes = document.querySelectorAll(".recette");

// Les titres et la saisie suivent la même transformation.
// Ainsi, «  SOUPE  » correspond à « Soupe de carottes ».
function normaliser(texte) {
  return texte.trim().toLocaleLowerCase("fr");
}

function filtrerRecettes() {
  const terme = normaliser(recherche.value);
  const limite = Number(dureeMax.value);
  let visibles = 0;

  cartes.forEach((carte) => {
    const titre = normaliser(carte.dataset.titre);
    const duree = Number(carte.dataset.duree);
    const titreOK = titre.includes(terme);
    // 0 représente le choix « Toutes les durées ».
    const dureeOK = limite === 0 || duree <= limite;
    const afficher = titreOK && dureeOK;
    carte.hidden = !afficher;
    if (afficher) {
      visibles += 1;
    }
  });

  compteur.textContent = `${visibles} recette${visibles > 1 ? "s" : ""} affichée${visibles > 1 ? "s" : ""}.`;
  aucun.hidden = visibles !== 0;
}

recherche.addEventListener("input", filtrerRecettes);
dureeMax.addEventListener("change", filtrerRecettes);
formulaire.addEventListener("submit", (evenement) => {
  // Entrée dans le champ ne doit pas recharger la page.
  evenement.preventDefault();
  filtrerRecettes();
});
formulaire.addEventListener("reset", () => {
  // L'événement reset précède le rétablissement des champs.
  // Le prochain tour de la boucle d'événements les lira une fois rétablis.
  setTimeout(filtrerRecettes, 0);
});

filtrerRecettes();
// Afficher les contrôles seulement après une initialisation réussie.
formulaire.hidden = false;

In the same series Step 3 of 6

  1. 1 Course HTML — The basics
  2. 2 Course CSS — The basics
  3. 3 Course JavaScript — The basics You are here
  4. 4 Course PHP — The basics
  5. 5 Course MySQL — The basics
  6. 6 Course PDO — The basics

Share this article

In the same category

Course

Development and production · Beginner

HTML — The basics

Build a semantic, accessible web page step by step with a running project: a recipe mini-blog.

4 guides · 38 concepts In the same series

Course

Development and production · Intermediate

Node.js — The fundamentals

Understand the Node.js runtime: modules, asynchronous code, npm and building a small server.

4 guides · 44 concepts In the same series

Course

Development and production · Beginner

CSS — The basics

Style the recipe mini-blog and make it responsive: selectors, box model, flexbox, grid and mobile-first.

4 guides · 39 concepts In the same series