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

245
Vistas
How to change a button's color when clicked on it and change it back to its original color when the button is clicked again?

In this case, my Upvote button is transparent, and when I click on it, its background color changes from transparent to green. I need to make this button transparent when I click on it again. I tried the following codes

function vote(){
  var upVoteBtn = document.getElementById("upVote");
  upVoteBtn.style.backgroundColor = "green";
  upVoteBtn.style.color = "black";
}
<button id="upVote" onclick=vote()>upVote</button> 

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

0

As @CBroe mentioned, instead of adding style in js, consider making a class with styles you need, toggle this class with a listener on button click.

document.getElementById("upVote").addEventListener("click", () => {
  event.target.classList.toggle("upvoted")
})
.upvoted{
  background-color:green;
}
<button id="upVote">upVote</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this!

var btnToggle = true;
var upVoteBtn = document.getElementById("upVote");

function vote(){
    if (btnToggle){
        upVoteBtn.style.backgroundColor = "green";
        btnToggle = false;
    } else{
        upVoteBtn.style.backgroundColor = null;
        btnToggle = true;
    }
}
<button class="btn btn-outline-success" id="upVote" onclick=vote()>upVote</button>

If you want to make it transparent, then simply use upVoteBtn.style.backgroundColor = "transparent"; instead of null. Just like below.

var btnToggle = true;
var upVoteBtn = document.getElementById("upVote");
upVoteBtn.style.backgroundColor = "transparent";

function vote(){
    if (btnToggle){
        upVoteBtn.style.backgroundColor = "green";
        btnToggle = false;
    } else{
        upVoteBtn.style.backgroundColor = "transparent";
        btnToggle = true;
    }
}
<button class="btn btn-outline-success" id="upVote" onclick=vote()>upVote</button>

Thanks and best regards!

about 4 years ago · Juan Pablo Isaza Denunciar

0

 function vote(element){
        element.classList.toggle("active");
}
                      
.btn.active{
  background:green;
  color: #fff;
}
.btn{
  background: transparent;
  color: black;
}
<button class="btn btn-outline-success" id="upVote" onclick=vote(this)>upVote</button> 


Solution of Problem

  • vote(this) send element of button
  • toggle() use to add and remove active class
  • .btn.active check button have active class or not
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