I am using few items in my bootstrap 3 navigation bar grouped in like below:
Just use:
$('.collapse').collapse();
First, add an id to your button tag.
<button id="idToggleButton" type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
Than add an id to your collapse div
<div id="divCollapse" class="collapse navbar-collapse navbar-ex1-collapse">
Finally you can programmatically click when divCollapse is visible
if ($('#divCollapse').is(":visible")){
$('#idToggleButton').click();
}
Or when is invisible
if (!$('#divCollapse').is(":visible")){
$('#idToggleButton').click();
}
add custom class to toggle navbar (me-toggle) so toggle variable with true value :
<a href="javascript:;" class="dropdown-toggle me-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true">
<i class="icon-envelope-open"></i>
<span class="badge badge-default"> 4 </span>
</a>
<ul class="dropdown-menu ">
<li class="external">
<h3>You have<span class="bold">7 New</span> Messages</h3>
<a href="app_inbox.html">view all</a>
</li>
<li>
item 1
</li>
<li>
item 2
</li>
</ul>
then in click navbar event :
toggle: true;
$('a.me-toggle').click(() => {
toggle = !toggle;
});
$('.dropdown').on({
"click": function(event) {
},
"hide.bs.dropdown": function(event) {
return toggle;
}
});
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
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>