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

175
Visualizações
I want to toggle .textDecoration class when I am clicking on "ALL" h1 elements. Can you Help me with this Code?

I can only access the first element in h1. I also can create a button and click loop through h1 elements and switch on or off .textDecoration class. But my aim is to click which element I want step by one and switch on or off .textDecoration class.

var x = document.getElementsByTagName("h1")[0];

function myFunction() {

  x.classList.toggle("textDecoration");
}

x.addEventListener("click", myFunction);
.textDecoration {
  text-decoration: line-through;
}
<h1>Text-Decoration: line-through</h1>
<h1>Text-Decoration: line-through</h1>
<h1>Text-Decoration: line-through</h1>
<h1>Text-Decoration: line-through</h1>

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

0

I suggest you delegate

document.getElementById("container").addEventListener("click", function(e) {
  const tgt = e.target.closest("h1");
  if (tgt) tgt.classList.toggle("textDecoration");
})
.textDecoration {
  text-decoration: line-through;
}
<div id="container">
  <h1>Text-Decoration: line-through</h1>
  <h1>Text-Decoration: line-through</h1>
  <h1>Text-Decoration: line-through</h1>
  <h1>Text-Decoration: line-through</h1>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Your question seems to me very poorly formulated, are you looking for that?

document.querySelectorAll('h1').forEach( (Hn,_,All_h1) =>
  {
  Hn.onclick =_=>
    {
    if (Hn.classList.toggle('textDecoration'))
      All_h1.forEach( Hx=> Hx.classList.toggle('textDecoration', Hn===Hx) )
    }  
  })
.textDecoration {
  text-decoration: line-through;
}
<h1>Text-Decoration: line-through</h1>
<h1>Text-Decoration: line-through</h1>
<h1>Text-Decoration: line-through</h1>
<h1>Text-Decoration: line-through</h1>

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