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

126
Visualizações
decrement and increment of a button using Jav

When my button is being clicked, it turns red. With that, the counter increases. However, I would like the counter to be dynamic. In such a way when the button is being deselected, the counter decreases too. However, I'm unable to do so. I did not receive any errors.

var buttons = document.getElementsByClassName("button");
var count = 0;
var disp = document.getElementById("display");
for (let i = 0, l = buttons.length; i < l; i++) {
  buttons[i].addEventListener('click', function() {
    buttons[i].classList.toggle('active');
    if (this.classList.contains("active")) {
      if (!this.classList.contains("active")) {
        count--;
        disp.innerHTML = count;
      }
      count++;
      disp.innerHTML = count;
    }

  })
}
.active {
  background-color: #E68352 !important;
}

.button {
  background-color: #FFFFFF;
}
<input type="button" id="button1" class="button" value="A" />
<input type="button" id="button2" class="button" value="B" />
<input type="button" id="button3" class="button" value="C" />

<p>
  Button Clicked <span id="display">0</span> Times
</p>

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

0

you have to use else, your second if can't never be reached the way you wrote this:

var buttons = document.getElementsByClassName("button");
var count = 0;
var disp = document.getElementById("display");
for (let i = 0, l = buttons.length; i < l; i++) {
  buttons[i].addEventListener('click', function() {
    buttons[i].classList.toggle('active');
    if (this.classList.contains("active")) {
      count++;
    } else {
      count--;
    }
    disp.innerHTML = count;
  })
}
.active {
  background-color: #E68352 !important;
}

.button {
  background-color: #FFFFFF;
}
<input type="button" id="button1" class="button" value="A">
<input type="button" id="button2" class="button" value="B">
<input type="button" id="button3" class="button" value="C">

<p>
  Button Clicked <span id="display">0</span> Times
</p>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do something like this:

var buttons = document.getElementsByClassName("button");
var count = 0;
var disp = document.getElementById("display");

for (let i = 0, l = buttons.length; i < l; i++) {
  buttons[i].addEventListener("click", function() {
    buttons[i].classList.toggle("active");

    if (this.classList.contains("active")) count++;
    else count--;

    disp.innerHTML = count;
  });
}
.active {
  background-color: #e68352 !important;
}

.button {
  background-color: #ffffff;
}
<input type="button" id="button1" class="button" value="A" />
<input type="button" id="button2" class="button" value="B" />
<input type="button" id="button3" class="button" value="C" />

<p>
  Button Clicked
  <span id="display">0</span> Times
</p>

The if else will check if the button contains the class "active" if it does it will increase the count by 1 or else if the button does not contain the class "active" it will decrease the count by 1.

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