dougmacklin
dougmacklin

Reputation: 2610

Bootstrap 3 Hide Dropdown Menu on Menu Item Click

Check out the fiddle. I have a basic Bootstrap 3 responsive nav like so:

    <div id="navbar" class="navbar navbar-fixed-top navbar-inverse">
        <div class="container">
            <a id="navbar-toggle" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>

            <a class="navbar-brand" href="#">Title Title Title</a>

            <div id="nav-collapse" class="nav-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a href="#option1">Option 1</a></li>
                    <li><a href="#option2">Option 2</a></li>
                    <li><a href="#option3">Option 3</a></li>
                </ul>
            </div>
        </div>
    </div>

The nav items link to sections on the page, rather than different pages so I need the dropdown to hide on click.

Whenever I try to toggle the dropdown manually with jQuery, it messes up future functionality of the dropdown toggle button:

$("#navbar li a").click(function(event) {
    // check if window is small enough so dropdown is created
    $("#navbar-toggle").is(":visible")
        $("#nav-collapse").toggle();
});

Is there a better fix for this?

Upvotes: 15

Views: 46145

Answers (6)

Roberto
Roberto

Reputation: 11

If need to fix this on bootstrap 4, try this jquery snippet. It works in this way: If navbar is expanded, nothing happens, (normal bootstrap behavior). If navbar is collapsed, after a click event, closes the navbar. Works on bootstrap 4

  $(".navbar a").click(function(event) {
      // check if window is small enough so dropdown is created
      var toggle = $(".navbar-toggler").is(":visible");
      if (toggle) {
          $(".navbar-collapse").collapse('hide');
      }
  });

Upvotes: 0

Mark
Mark

Reputation: 2549

With Bootstrap 3.3.4 I realized my old way quit working. Updated it to:

if ($('div.navbar-collapse').hasClass('in')) {
    $('button.navbar-toggle:visible').click();
}

Upvotes: 2

RMattos
RMattos

Reputation: 21

I am new to Bootstrap but with a small changed to the above code it works beautifully now. Remember that when you click on the logo the drop-down menu should close too, that is why I have added navbar-header to it!

jQuery(document).ready(function () {
        jQuery(".nav a, .navbar-header a").click(function(event) {
                // check if window is small enough so dropdown is created
               jQuery(".navbar-collapse").removeClass("in").addClass("collapse");
        });
});

Upvotes: 2

Charly
Charly

Reputation: 851

If you wanted to do this without JavaScript you could simply add the data-target and data-toggle to each list item, like so:

<li data-toggle="collapse" data-target=".nav-collapse"><a href="#option1">Option 1</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#option2">Option 2</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#option3">Option 3</a></li>

For the navbar, this really only works in the mobile view mode when there is a toggle button. Strange things happen when the navbar is exposed (the toggle button isn't showing).

Upvotes: 24

bhall
bhall

Reputation: 1401

I don't know why clicking/tapping on a menu item in a menu dropdown in a collapsed navbar doesn't automatically toggle the menu in Bootstrap. There may be a more graceful way to do this, but, here's how I fixed it (using Bootstrap 3.0.3):

<li class="visible-xs">
  <a href="#" data-toggle="collapse" data-target=".navbar-collapse">Link</a>
</li>
<li class="hidden-xs">
  <a href="#">Link</a>
</li>

Basically, there are two versions of the link, one that appears whenever the menu toggle is shown (at browser widths <768px) that will toggle the navbar menu, and one that doesn't collapse it (browser widths >768px). If you don't add that second type, it will show a strange phantom animated horizontal scroll bar when trying to toggle the menu when it is not there.

Upvotes: 3

Gloria
Gloria

Reputation: 1053

Change this:

$(document).ready(function () {
    $("#navbar li a").click(function(event) {
        // check if window is small enough so dropdown is created
        $("#navbar-toggle").is(":visible")
            $("#nav-collapse").toggle();
    });
  });

to this:

$(document).ready(function () {
    $("#navbar li a").click(function(event) {
        // check if window is small enough so dropdown is created
    $("#nav-collapse").removeClass("in").addClass("collapse");
    });
});

See http://jsfiddle.net/fw7vh/4/

Upvotes: 8

Related Questions