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
Guides
- 01 Guide JavaScript glossary Explain the concepts in your own words and recognise them in an example.
- 02 Guide JavaScript essentials Choose and use the essential elements to complete this step of the blog.
- 03 Guide JavaScript workshop · À table ! Reproduce a working result, test it, then succeed at an exercise without copying.
- 04 Guide Resources and review Find reliable information and explain what you have built.
Suggested path
- 01. HTML: structure three recipes in a semantic page.
- 02. CSS: make the blog readable on mobile and on desktop.
- 03. JavaScript: filter the recipes without reloading the page.
- 04. PHP: produce the pages from arrays and process a form.
- 05. MySQL: keep the recipes in a relational database.
- 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 Course HTML — The basics
- 2 Course CSS — The basics
-
3 Course JavaScript — The basics You are here
- 4 Course PHP — The basics
- 5 Course MySQL — The basics
- 6 Course PDO — The basics
In the same category
Development and production · Beginner
HTML — The basics
Build a semantic, accessible web page step by step with a running project: a recipe mini-blog.
Development and production · Intermediate
Node.js — The fundamentals
Understand the Node.js runtime: modules, asynchronous code, npm and building a small server.
Development and production · Beginner
CSS — The basics
Style the recipe mini-blog and make it responsive: selectors, box model, flexbox, grid and mobile-first.