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

150
Visualizações
Facing problem with getting JS button to work

I made a button and added event listener for click to it. right now I set the following code for JS (button id is next):

next.addEventListener("click", () => {
  if (AB.style.backgroundColor === "pink") {
    AB.style.backgroundColor = "red";
  } else if (AB.style.backgroundColor === "red") {
    AB.style.backgroundColor = "orange";
  }
});
#AB {
  background-color: pink;
}
<div id="AB">Words</div>
<button id="next">Button</button>

Now when I click the button the first time, the color does change from pink to red. However, the second click (namely the else if part) does not do anything. How can I make the color toggle on click?

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

0

Yep, as suggested @ggorlen Look into getComputedStyle().

But it will give you a computed value, which is in rgb(,,,) format.

I'd recommend using classes instead.

<!DOCTYPE html>
<html>

<head>
  <style type="text/css">
    .pink {
      background-color: pink;
    }
    .red {
      background-color: red;
    }
    .orange {
      background-color: orange;
    }
  </style>
</head>

<body>
  <div id="AB" class="pink">Words</div>
  <button id="next">Button</button>
</body>
<script>
  next.addEventListener("click", () => {
    const element = document.getElementById('AB');

    if (element.classList.contains('pink')) {
      element.classList.remove('pink');
      element.classList.add('red');
    } else if (element.classList.contains('red')) {
      element.classList.remove('red');
      element.classList.add('orange');
    }
  });
</script>

</html>
about 4 years ago · Juan Pablo Isaza Relatório

0

The problem here is that it seems that your div does not have any style.backgroundColor value by default.

To face this problem you can set the backgroundColor value dynamically when the javascript is loaded.

AB.style.backgroundColor = "pink"
next.addEventListener("click", function () {
  if (AB.style.backgroundColor === "pink") {
    AB.style.backgroundColor = "red";
  } else if (AB.style.backgroundColor === "red") {
    AB.style.backgroundColor = "orange";
  }
});
<div id="AB">Words</div>
<button id="next">Button</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

As ggorlen commented you can get the style by using the getComputedStyle() and then compare it against the CSS background-color property. Check out getComputedStyle on MDN.

next.addEventListener("click", () => {
  if (getComputedStyle(AB).getPropertyValue("background-color") === "rgb(255, 192, 203)") {
    AB.style.backgroundColor = "red";
  } else if (AB.style.backgroundColor === "red") {
    AB.style.backgroundColor = "orange";
  }
});
#AB {
  background-color: pink;
}
<div id="AB">Words</div>
<button id="next">Button</button>

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