Skip to content

Instantly share code, notes, and snippets.

@Vincent-gv
Last active December 9, 2018 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vincent-gv/6a80af8b705911e5770afd7547f92aae to your computer and use it in GitHub Desktop.
Save Vincent-gv/6a80af8b705911e5770afd7547f92aae to your computer and use it in GitHub Desktop.
Changer la couleur des divs selon la touche pressée : https://oc-courses.github.io/javascript-web/chapitre_5/html/couleurs.html
/*
Changer la couleur des divs :
*/
document.addEventListener("keypress", function (e) {
var touche = String.fromCharCode(e.charCode); // Récupération de la touche pressée
touche = touche.toUpperCase(); // Pour gérer indifféremment minuscules et majuscules
var couleur = "";
switch (touche) {
case "B":
couleur = "white";
break;
case "J":
couleur = "yellow";
break;
case "V":
couleur = "green";
break;
case "R":
couleur = "red";
break;
default:
console.log("Touche " + touche + " non gérée");
}
// Changement de couleur de fond pour toutes les divs
var divs = document.getElementsByTagName("div");
for (var i = 0; i < divs.length; i++) {
divs[i].style.backgroundColor = couleur;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment