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

146
Visualizações
Want to check for all DIV ID and match with anchor link in UL LI a[href], if match found then add a class to the LI items

I really am facing a complex situation here.
I have created an event page. Each page will have multiple sections like CULTURE, FOOD, SPORTS and each section will have events. I already set a system that has a nav tag, inside the nav tag we have ul li a[#anchor-links]. I have this code that will look for Div id when it is visible it will add an active class to that list item.
Now, in the sidebar, under each li item, I create 12 more li items for months. That will DISPLAY NONE initially and display inline-block when the mother LI has a class ACTIVE. What I am trying is to find all DIV ID for a month as well, and compare with these 12 li a[href], then months we have events on, I want that those link active and show a special class.
These are ID I added
Culture - #culture . Events under culture like #culture-jan,#culture-feb
Food - #food. Events under food like #food-jan,#food-fed ....
This is the JQuery I am using so far

<script>
window.addEventListener('DOMContentLoaded', () => {

  const observer = new IntersectionObserver(entries => {
    entries.forEach(entry => {
      const id = entry.target.getAttribute('id');
      if (entry.intersectionRatio > 0) {
        document.querySelector(`nav li a[href="#${id}"]`).parentElement.classList.add('active');
      } else {
        document.querySelector(`nav li a[href="#${id}"]`).parentElement.classList.remove('active');
      }
    });
  });

  // Track all sections that have an `id` applied
  document.querySelectorAll('div[id]').forEach((div) => {
    observer.observe(div);
  });
  
});
</script>


Please anyone, help. Thanks
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