I am working with a navbar:
<nav id="navbar" class="navbar fixed-top navbar-expand-md navbar-light bg-light">
<a class="navbar-brand" href="#"></a>
<button id="toggle" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse text-right" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" id="about" href="Gibbs.html"><span><strong>ABOUT</strong></span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><span>NEWS</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><span>LAB MEMBERS</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><span>RESEARCH</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="publications.html"><span>PUBLICATIONS</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><span>OPPORTUNITIES</span></a>
</li>
</ul>
</div>
</nav>
I am trying to find a way in JavaScript to detect whether the navbar is collapsed or expanded, so that I can style other elements on the page accordingly. The code snippet I attempted to use is as follows:
if ($('#toggle').attr('aria-expanded') == "true") {
console.log("true")
}
However, this code does not produce the desired outcome. Is there a more effective way to achieve this?