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

106
Visualizações
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 Respostas
Responde à pergunta

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