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

141
Vistas
Running js function on table

How do I make it change each element to a random color instead of changing all the elements to the same random color

function RandomColor() {
const randomColor = Math.floor(Math.random()*16777215).toString(16);
var th = document.getElementsByTagName('th');
for (i = 0; i < th.length; i++) {
    x = th[i]
x.style.backgroundColor = "#" + randomColor;
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to set the random color in the iteration so it resets for every element.

function RandomColor() {
  var th = document.getElementsByTagName('th');
  for (i = 0; i < th.length; i++) {
    const randomColor = Math.floor(Math.random() * 16777215).toString(16);
    x = th[i]
    x.style.backgroundColor = "#" + randomColor;
  }
}

RandomColor();
<table>
  <tr>
    <th>Lorem</th>
    <th>Ipsum</th>
  </tr>
  <tr>
    <td>foo</td>
    <td>bar</td>
  </tr>
</table>

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