Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!