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

154
Visualizações
Javascript to hide an element based on the contents of an active-class element elsewhere

I'm trying to hide some elements on a page based on the contents of the specific element in a navbar (formed as a list) which has an active class. Here's what I've tried so far.

var element=document.querySelector('ul.nav > li.active')
if((typeof element != 'undefined')?element.innerText == "Page 1")
{
   document.getElementById("options1").style.display = 'none';

}

And the HTML that I need to work with is basically as follows.

<ul class="nav navbar-nav navbar-left">
 <li class="active">
  <a href="#section-page-1">Page 1</a>
 </li>
 <li>
  <a href="#section-page-2">Page 2</a>
 </li>
 <li>
  <a href="#section-page-3">Page 3</a>
 </li>
</ul>

<div id="options1">Stuff Here</div>

The behavior that I want is that when Page 1 is active, the <div> is hidden, and when all other pages are active, the <div> is visible.

I've tried a bunch of closely related things from stack overflow but haven't managed to get anything to work yet.

Thanks! I'm pretty new to javascript.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can get the # called by your link with window.location.hash is JS like

if (window.location.hash === "#section-page-1") {
  document.getElementById("options1").style.display = 'none';
}

This should work

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