Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

425
Vistas
How to toggle CSS style on-click same button in JS?

How to toggle CSS div background style on clicking the same button?

function changeBg() {
  var divElem = document.getElementById("change-bg");
  divElem.style.backgroundColor = "red";
}
body {
  margin: 0;
}

#change-bg {
  /* Extra Text Formatting */
  padding: 10px;
  text-align: center;
  /* Primary Background Color */
  background-color: yellow;
}
<div id="change-bg">
  <h3>
    Hello World
    <br/>
    <button onclick="changeBg();">Change Color</button>
  </h3>
</div>

It Changes color to red, but how do I revert its original color (yellow) on clicking the same button

Check out the JSFiddle. I think It can be solved using If-Else, I'm not able to implement it.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use classList.toggle to change class name string and change it back when classList already has with this following code

const box = document.querySelector(".bg")
const btn = document.querySelector(".btn")

btn.addEventListener("click", () => {
  box.classList.toggle("blue")
})
.bg{
  background: red;
  width: 100px;
   height: 100px;
}

.blue{
  background: blue
}
<div class="bg"></div>
<button class="btn">Toggle it</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here's an alternative with a toggle variable and the if-else solution mentioned in the OP.

const divElem = document.getElementById("change-bg");
let toggle = true;

function changeBg() {
  divElem.style.backgroundColor = toggle ? 'red' : 'yellow'
  toggle = !toggle;
}
body {
  margin: 0;
}

#change-bg {
  padding: 10px;
  text-align: center;
  background-color: yellow;
}
<div id="change-bg">
  <h3>
    Hello World
    <br/>
    <button onclick="changeBg();">Change Color</button>
  </h3>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use This Methhod as it is:)

function changeBg() {
  var divElem = document.getElementById("change-bg");
  divElem.classList.toggle("change-color");
}
body {
  margin: 0;
}

#change-bg {
  /* Extra Text Formatting */
  padding: 10px;
  text-align: center;
  /* Default Background Color */
  background-color: yellow;
}
.change-color{
  background-color: red !important;
}
<!DOCTYPE html>

<head>
</head>

<body>
  <div id="change-bg">
    <h3>
      Hello World
      <br/>
      <button onclick="changeBg();">Change Color</button>
    </h3>
  </div>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda