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

305
Vistas
How do I use the document.getElementsByClassName().style to change the color of text?

So I tried to make a Light Mode function. But to do that, I need to change the color of some elements, but I have assigned classes to them. Someone tell me how to use the document.getElementsByClassName() function to change the color of elements assigned that class?

I tried doing this (see second code snippet) but it only effects 1 Element. I have 2 of the same element with the same class on a page.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>title</title>
  <style>
        .myH1{
           color:black;
</head>

<body>
<script>
function lightMode(){
      document.body.style.background = "white";
      document.getElementsByClassName("myH1").style.color="blue";
</script>
      
  <h1 class="myH1">Header</h1>
  <button onclick="lightMode()">☀</button>
  
</body>

</html>
function lightMode()
       document.body.style.background="white";
       const element = document.getElementsByClassName("myH1")
       element.style.color="black";
about 4 years ago · Juan Pablo Isaza
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