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

139
Vistas
¿Cómo cambio el color del texto de un botón en HTML a un color aleatorio al hacer clic en él?

He probado el siguiente código:

 <button id = "changeColor">Click here to change my color</button> <script> var changeButton = document.getElementById('changeColor'); function random(number){ return Math.floor(Math.random()*number); } function switchColor(){ var randomColor = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')'; } changeButton.addEventListener('click', switchColor); </script>

pero el botón no devuelve ningún color al hacer clic en él.

Muchas gracias de antemano por responder a mi pregunta.

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

0

Debe cambiar el color del style del botón seleccionado con el color aleatorio generado.

 changeButton.style.color = randomColor 

 <button id = "changeColor">Click here to change my color</button> <script> var changeButton = document.getElementById('changeColor'); function random(number){ return Math.floor(Math.random()*number); } function switchColor(){ var randomColor = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')'; changeButton.style.color = randomColor console.log(randomColor); } changeButton.addEventListener('click', switchColor); </script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

<button id = "changeColor">Click here to change my color</button> <script> var changeButton = document.getElementById('changeColor'); function random(number){ return Math.floor(Math.random()*number); } function switchColor(){ var randomColor = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')'; // CHANGE THE TEXT COLOR document.getElementById("changeColor").style.color = randomColor; } changeButton.addEventListener('click', switchColor); </script>
about 4 years ago · Juan Pablo Isaza Denunciar

0

Prueba de esta manera

Falta tu código

 changeButton.style.color = color; 

 var changeButton = document.getElementById('changeColor'); function random(min, max){ return Math.floor(Math.random() * (max - min + 1)) + min; } function switchColor(){ var color; color = 'rgb(' + random(0, 255) + ',' + random(0, 255) + ',' + random(0, 255) + ')'; changeButton.style.color = color; } changeButton.addEventListener('click', switchColor);
 button{ border: none; }
 <button id = "changeColor">Click here to change my color</button>

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