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

104
Vistas
How to remove a class form from the previous and the next sibling label of the currently active label with javascript

I have a function to add a class civil-active to the label that is clicked. When the next label is clicked the class is added to it too, naturally. But I want the class civil-active to be there only on the clicked label and get it removed from its previous and next sibling if present.

the code that is adding the class is

js

var labels = document.getElementsByTagName("label");

for (let i = 0; i < labels.length; i++) {
  labels[i].addEventListener("click", addclass);
}

function addclass(event) {
  event.target.classList.add("civil-active");
}

Please suggest me what should I do

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In the event handler, find the element having the class and remove it.

function addclass(event) {

  // Those lines will remove the class on the element that has it (if there is one)
  let active = document.querySelector(".civil-active")
  if(active){
    active.classList.remove("civil-active");
  }
  event.target.classList.add("civil-active");
}
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