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

265
Visualizações
How to change the font color of a list in Javascript?

So, I'm trying to make these nav-links of mine to change its color whenever I click a button, but it does not seem to work.

   <ul class="nav-links">
         <li id = link><a href="#">Home</a></li>
         <li id = link><a href="#">Projects</a></li>
         <li id = link><a href="#">About</a></li>
         <li id = link><a href="#">Contacts</a></li>
   </ul>
   <label class="toggle">
         <input type="checkbox" class="check">
         <span class="slider" onclick="dark()"></span>
   </label>

I tried doing this script but it still doesn't work

  const text = document.querySelector(".logo");
  const body = document.querySelector("body");
  const banner = document.querySelector(".L-banner");
  const navLinks = document.getElementById("#link");
  function dark(){
      text.style.color = "#fff";
      body.style.background = "#000";
      banner.style.color = "#fff";

      for(var i = 0; i < navLinks.length; i++){
        navLinks[i].style.color = "#fffff";
      }
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can't give multiple elements the same ID, and querySelector only selects a single element.

To select all the links, you can use querySelectorAll

document.getElementById('button').addEventListener('click', changeColor);
function changeColor() {
    const links = [...document.querySelectorAll('.nav-links li a')];
    links.forEach(link => link.style.color = "cornflowerblue");
}
<ul class="nav-links">
    <li>
        <a href="#">Home</a>
    </li>
    <li>
        <a href="#">Projects</a>
    </li>
    <li>
        <a href="#">About</a>
    </li>
    <li>
        <a href="#">Contacts</a>
    </li>
</ul>
<button id="button">Change link color</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

you should change it to this

      const navLinks = document.querySelectorAll("#link a");

also don't forget to fix html

<ul class="nav-links">
      <li id="link"><a href="#">Home</a></li>
      <li id="link"><a href="#">Projects</a></li>
      <li id="link" ink><a href="#">About</a></li>
      <li id="link" ink><a href="#">Contacts</a></li>
    </ul>

about 4 years ago · Juan Pablo Isaza Relatório
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