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

163
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
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