Programmatically toggle bootstrap 3 navigation bar
Solution 1
After a little bit of help from @Jahnux73 I figured it out myself. So the only thing I had to do is to add :
data-toggle="collapse" data-target=".navbar-ex1-collapse"
to the specific link that I wanted to toggle the navbar. so the link now looks like following:
<a href="#" id="online-offline" data-toggle="collapse"
data-target=".navbar-ex1-collapse" toggle="offline">
<span class="glyphicon glyphicon-off"></span> Go offline
</a>
Solution 2
As you said, the event will run a modal.
So, when your modal ( called yourModal ) is showing (before showing), just hide the menu :
JS :
$('.yourModal').on('show.bs.modal', function(){
$('.navbar-collapse').collapse('hide');
});
Here are the docs :
http://getbootstrap.com/javascript/#collapse
http://getbootstrap.com/javascript/#modals-usage
Comments
-
bboydflo about 2 years
I am using few items in my bootstrap 3 navigation bar grouped in like below:
<nav class="navbar navbar-inverse navbar-fixed-top" id="toggleNav" role="navigation"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Brand name</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul class="nav navbar-nav navbar-right"> <li> <a href="javascript:logout();"><span class="glyphicon glyphicon-log-out"></span> Logout</a> </li> <li> <a href="#" id="online-offline" toggle="offline"><span class="glyphicon glyphicon-off"></span> Go offline</a> </li> </ul> </div><!-- /.navbar-collapse --> </nav>
When I click on the second item ('Go offline') I use jquery to invoke a modal window. On mobile I have to first collapse the menu to reach the link, and then when I click on that item I get my modal window correctly.
What I need to achieve is to hide the navbar collapse before showing the modal. Is it possible to programmatically toggle the navbar collapse? How can I do that ?
-
Wayne Wei almost 4 yearsThank you. this is the best way among all the solutions i found.