My learning of JavaScript is full of pitfalls and I don't really understand the logic of my exo. I want to use document.querySelectorAll to change the size of my rectangles to "large" with the same function I used before (function reDisplay() { ... }) with a variable let rectsAreBig = true.
I have to use a variable that will represent the "current state of the page".
To do this I must use custom variables (which I will use to represent the "state" of the rectangles):
I need to call the function reDisplay();
My style is
.rect {
background: red;
width: 250px;
height: 250px;
margin: 10px;
float: left;
}
.rect.blue {
background: blue;
}
.rect.yellow {
background: yellow;
}
.rect.green {
background: green;
}
.smallRect {
width: 150px;
height: 150px;
margin: 10px;
}
HTML:
<div class="rectContainer">
<div id="rect1" class="rect red">
</div>
<div id="rect2" class="rect red">
</div>
<div id="rect3" class="rect red"></div>
</div>
My first rectangle should change colour with document.querySelector and my code is this and it works even if I am not sure if I do follow the recommendations.
let stateRect1Color = "blue";
let rectsAreBig = true;
let rectContainer = document.querySelector(".rect");
console.log(rectContainer);
rectContainer.addEventListener("click", function reDisplay() {
for (let couleurs = 0; couleurs < 5; couleurs++) {
console.log(couleurs);
let lePremierRectangle = document.querySelector("#rect1");
switch (stateRect1Color) {
case "blue":
stateRect1Color = "red";
// document
// .querySelector("#rect1")
lePremierRectangle.classList.remove("blue");
// document
// .querySelector("#rect1")
lePremierRectangle.classList.add("red");
break;
case "red":
stateRect1Color = "yellow";
// document
// .querySelector("#rect1")
lePremierRectangle.classList.remove("red");
// document
// .querySelector("#rect1")
lePremierRectangle.classList.add("yellow");
break;
case "yellow":
stateRect1Color = "green";
// document
// .querySelector("#rect1")
lePremierRectangle.classList.remove("yellow");
// document
// .querySelector("#rect1")
lePremierRectangle.classList.add("green");
break;
case "green":
stateRect1Color = "blue";
// document
// .querySelector("#rect1")
lePremierRectangle.classList.remove("green");
// document
// .querySelector("#rect1")
lePremierRectangle.classList.add("blue");
break;
default:
console.log();
}
console.log(stateRect1Color);
}
and my second rectangle code doesn't work to change the shape
let rectContainer2 = document.querySelectorAll(".rect");
rectContainer2.addEventListener("click", function reDisplay() {
let leDeuxiemeRectangle = document.querySelector("#rect2");
for (let i = 0; i < TousLesRectangles.length; i++) {
if (rectsAreBig == true) {
console.log((rectsAreBig = true));
rectsAreBig = false; /
// On récupère le rectangle 2
leDeuxiemeRectangle.classList.remove(".smallRect");
console.log(smallRect);
leDeuxiemeRectangle.classList.add(".smallRect");
console.log(); // On affiche la valeur de la variable rectsAreBig dans la console
} else {
rectsAreBig = true;
leDeuxiemeRectangle.classList.remove(".smallRect");
leDeuxiemeRectangle.classList.add(".smallRect");
console.log();rectsAreBig dans la console
}
console.log(rectsAreBig);
}
});