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

327
Visualizações
How to manipulate multiple element's styles with a single event listener?

This is probably very simple, but I cannot make it work. I have 2 headings with the same class

<h1 class="heading-title">Hyperspace</h1>
<h1 class="heading-title">Hyperspace</h1>

I also have a button that I want to change both the headings color when I click it

const customColor1Btn = document.querySelector(".custom-color-1");
const headings = document.querySelectorAll(".heading-title");

customColor1Btn.addEventListener("click", function () {
  headings.style.color = "red";
});

It works when I change the color of just 1 heading, but I can never make an event listener work with more than 1 element. I need 1 button click to manipulate multiple element's styles.

How can I make it work?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Document.querySelectorAll returns a NodeList

So, you need to loop over all the headings in the headings NodeList and change the color for the individual heading nodes.

const customColor1Btn = document.querySelector(".custom-color-1");
const headings = document.querySelectorAll(".heading-title");

customColor1Btn.addEventListener("click", function () {
  headings.forEach(h => h.style.color = "red");
});
<h1 class="heading-title">Hyperspace</h1>
<h1 class="heading-title">Hyperspace</h1>
<button class="custom-color-1">Change color</button>

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