Question

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>&nbsp;Logout</a>
                </li>
                <li>
                    <a href="#" id="online-offline" toggle="offline"><span class="glyphicon glyphicon-off"></span>&nbsp;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 ?

Was it helpful?

Solution 2

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>&nbsp;Go offline
</a>

OTHER TIPS

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

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;
          }
      });

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top