Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
¿Cómo cambiar el color de un botón cuando se hace clic en él y volver a cambiarlo a su color original cuando se vuelve a hacer clic en el botón?

En este caso, mi botón Upvote es transparente y cuando hago clic en él, su color de fondo cambia de transparente a verde. Necesito hacer que este botón sea transparente cuando vuelva a hacer clic en él. Probé los siguientes códigos

 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 answers
Answer question

0

Como mencionó @CBroe, en lugar de agregar estilo en js, considere crear una clase con los estilos que necesita, toggle esta clase con un listener al hacer clic en el botón.

 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 Report

0

¡Prueba esto!

 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>

Si desea hacerlo transparente, simplemente use upVoteBtn.style.backgroundColor = "transparent"; en lugar de null . Al igual que a continuación.

 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>

¡Gracias y un saludo!

about 4 years ago · Juan Pablo Isaza Report

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) enviar elemento de botón
  • toggle() usar para agregar y eliminar la clase activa
  • El botón de verificación .btn.active tiene clase activa o no
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!