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

217
Visualizações
Why doesn't my class names update when set though JS?

This may be a really simple problem but I can't seem to find why this is happening. I'm trying to develop a SPA in vanilla js using webpack, so far I was able to implement routing with hashchange event and triggering rerendering. But when I tried to add an active class to the relevant link though when the hash changes, It doesn't work. But when I log to the console, it seems that class was added successfully, but in the HTML it doesn't get updated. Why is this?

this is my hashchange listener,

  window.addEventListener("hashchange", (e) => {
  const hash = window.location.hash.replace("#", "");
  const view = routes.find((route) => {
    return route.path == hash;
  });
  const links = document.querySelectorAll(".nav-list--link");
  app.render(view.name);
  links.forEach((l) => {
    const hashHref = l.getAttribute("href").replace("/#", "");
    if (hash === hashHref) {
      l.classList.add("active");
      console.log(l, l.classList);
    } else {
      l.classList.remove("active");
      console.log(l, l.classList);
    }
   });
 });

And this is the console output,

Console output when I click the home link

This is the HTML,

I don't understand why it doesn't update in the HTML if it's shown as updated in Javascript

about 4 years ago · Juan Pablo Isaza
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