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

146
Views
¿Cómo programo una función de javascript para cambiar los colores de los botones de un lado a otro?

Estoy tratando de convertir un botón azul en rojo usando un clic, pero también quiero que el botón vuelva a ser azul después de hacer clic nuevamente usando la misma función de clic.

¿Cómo haría esto?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .round { border-radius: 5px; color: aliceblue; } .blue { background-color: blue; } .red { background-color: red; } </style> </head> <body> <button id="btn" class="round blue" onclick="clickBtn()">button</button> <script> function clickBtn() { let btn = document.getElementById('btn') if(btn.classList.contains('blue')) { btn.classList.remove('blue') btn.classList.add('red') } else { btn.classList.remove('red') btn.classList.add('blue') } } </script> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

Puede darle al botón un color de fondo predeterminado, luego agregarle un detector de eventos de click que alterna una clase que aplica un color de fondo diferente:

 document.querySelector('button').addEventListener('click', function(){ this.classList.toggle("red") })
 button{ background-color:green; } .red{ background-color:red; }
 <button>Hello World!</button>

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!