Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

346
Visualizações
once all divs have been clicked how do you change the CSS

I'm trying to change the background color for all the divs once they have been clicked. This code allows the change to happen but I can click on the same div 3 times and get the same output. Anyone know a good solution to this? Thank you!

        var boxes = document.querySelectorAll('.boxes')
        var box1 = document.querySelector('#box1')
        var box2 = document.querySelector('#box2')
        var box3 = document.querySelector('#box3')
        var clickCounter = 0
        
        function handleColor (box){
            box.style.backgroundColor = 'red';
            clickCounter = clickCounter + 1
            console.log(clickCounter);
            if (clickCounter > 2) {
                reset()
            }
        }
        
        for (let i = 0; i < boxes.length; i++) {
            boxes[i].addEventListener("click", () => handleColor(boxes[i]));
               
        }
        
        function reset() {
            box1.style.backgroundColor = 'green'
            box2.style.backgroundColor = 'green'
            box3.style.backgroundColor = 'green'
        }
.boxes {
  width: 200px;
  height: 200px;
  outline: 1px solid black;
  }
    <div class="boxes" id="box1"></div>
    <div class="boxes" id="box2"></div>
    <div class="boxes" id="box3"></div>
   

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You will need to flag each respective box and in order to control if it has been clicked at least once.

In this case you can use the class .clicked both as a flag in order to control whether you should reset or not, but for styling as well

var boxes = document.querySelectorAll('.boxes');
var box1 = document.querySelector('#box1');
var box2 = document.querySelector('#box2');
var box3 = document.querySelector('#box3');

function handleColor (box){
    box.classList.add('clicked');

    if (box1.classList.contains("clicked") && box2.classList.contains("clicked") && box3.classList.contains("clicked")) {
        reset();
    }
}

for (let i = 0; i < boxes.length; i++) {
    boxes[i].addEventListener("click", () => handleColor(boxes[i]));    
}

function reset() {
    box1.style.backgroundColor = 'green';
    box2.style.backgroundColor = 'green';
    box3.style.backgroundColor = 'green';

    for (let i = 0; i < boxes.length; i++) {
        boxes[i].classList.remove('clicked');    
    }
}
.boxes {
  width: 200px;
  height: 200px;
  outline: 1px solid black;
}

.boxes.clicked {
  background: red;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to remember for each box whether it has been clicked or not and then on clicking a box go through all the others to see if they have all been clicked.

There are several ways of remembering: using a CSS class, within JS or within the HTML.

This snippet remembers by adding an attribute 'hasBeenClicked' with value 1 to a box element when it is clicked.

var boxes = document.querySelectorAll('.boxes')
var box1 = document.querySelector('#box1')
var box2 = document.querySelector('#box2')
var box3 = document.querySelector('#box3')

function handleColor(box) {
  box.style.backgroundColor = 'red';
  box.setAttribute('hasBeenClicked', 1);
  let clickCounter = 0;
  boxes.forEach(box => {
    clickCounter += (box.getAttribute('hasBeenClicked') == 1) ? 1 : 0;
  });
  if (clickCounter == boxes.length) {
    reset()
  }
}

for (let i = 0; i < boxes.length; i++) {
  boxes[i].addEventListener("click", () => handleColor(boxes[i]));

}

function reset() {
  box1.style.backgroundColor = 'green'
  box2.style.backgroundColor = 'green'
  box3.style.backgroundColor = 'green'
}
.boxes {
  width: 200px;
  height: 200px;
  outline: 1px solid black;
}
<div class="boxes" id="box1"></div>
<div class="boxes" id="box2"></div>
<div class="boxes" id="box3"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda