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

213
Visualizações
How do I remove the class from the navbar button when a new link is selected [Javascript]

I am trying to make an active state on my link when clicked, which remains until another link is clicked. I'd like to have the class removed and transferred to the next link when clicked.

Currently it can be toggled on and off on each link.

// navbar toggle for active state
document.getElementById('navbar__list').addEventListener("click", function(event){
  event.target.classList.toggle('funcElemStyle');
});
.funcElemStyle {
  background: #333;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Instead of using javascript to achive this functionality, you can use CSS to achieve the same effect by using :active.

for example:

/* Selects the link element. */
a:active {
    color: #4444AA;
}

you can also use :hover if you want effects when hovered. CSS also allows chaining so if you want an effect to apply when it is hovered over and active, you can use :active:hover

about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of toggle use .add() and .remove() functions. Run snippet for an example.

document.querySelector("div").addEventListener("click", event=>{
  event.target.classList.remove("red")
})
div{
  width: 100px;
  height: 100px;
  background: green;
}
.red{
  background: red;
}
<div class="red"></div>

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