Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

302
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda