125

I have a more or less standard navigation from bootstrap 3

<body data-spy="scroll" data-target=".navbar-collapse">
    <!-- ---------- Navigation ---------- -->
    <div class="navbar navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                </button>
                <a class="navbar-brand" href="index.html"> <img src="#"></a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li class="active"><a href="#home">Home</a></li>
                    <li><a href="#one">One</a></li>
                    <li><a href="#two">Two</a></li>
                    <li><a href="#three">Three</a></li>
                    <li><a href="#four">Four</a></li>
                </ul>
            </div>
        </div>
    </div>

It collapses normally on smaller devices. Clicking on the menu button expands the menu but if I click (or touch for that matter) on a menu link, the menu stays open. What do I have to do, to close it again?

MERose
  • 4,048
  • 7
  • 53
  • 79
Paranoia
  • 2,040
  • 5
  • 25
  • 27
  • 5
    the answer of user3669917 is certainly the easiest and most straight forward answer! – Thomas Apr 03 '15 at 16:17
  • 1
    Possible duplicate of [Hide Twitter Bootstrap nav collapse on click](http://stackoverflow.com/questions/16680543/hide-twitter-bootstrap-nav-collapse-on-click) – Inanc Gumus Dec 14 '16 at 21:31

24 Answers24

138

Just to share this from solutions on GitHub, this was the popular answer:

https://github.com/twbs/bootstrap/issues/9013#issuecomment-39698247

$(document).on('click','.navbar-collapse.in',function(e) {
    if( $(e.target).is('a') ) {
        $(this).collapse('hide');
    }
});

This is wired to the document, so you don't have to do it on ready() (you can dynamically append links to your menu and it will still work), and it only gets called if the menu is already expanded. Some people have reported weird flashing where the menu opens and then immediately closes with other code that did not verify that the menu had the "in" class.

[UPDATE 2014-11-04] apparently when using sub-menus, the above can cause problems, so the above got modified to:

$(document).on('click','.navbar-collapse.in',function(e) {
    if( $(e.target).is('a:not(".dropdown-toggle")') ) {
        $(this).collapse('hide');
    }
});
Aaron
  • 2,049
  • 4
  • 28
  • 35
Kevin Nelson
  • 7,613
  • 4
  • 31
  • 42
  • 1
    Works flawlessly. Thanks for sharing! – Rodrigo Chiong May 28 '15 at 00:46
  • Can you explain '$(this).collapse('hide');' please. i don't understand where 'collapse' comes from – timebandit Aug 21 '15 at 20:53
  • @ImranNazir, sorry this is way after the question, but `collapse` is a method defined by bootstrap and attached to the jQuery object...presumably, they re-use this for multiple purposes (accordion, navbar, etc): http://getbootstrap.com/javascript/#collapse-usage – Kevin Nelson Oct 27 '15 at 15:03
  • Updated code worked for me, answers above this did not. Thanks! – RandomUs1r Jun 23 '17 at 19:17
  • The "[UPDATE 2014-11-04]" worked. As for the other solution where it recommend to remove duplicate reference to bootstrap.js and jquery that is not necessary the solution. I found this solution to fix the problem with less time to waist on debugging and with more explicit behavior. Thank you!. I hope this helps you as it did help me. – Nour Lababidi Oct 06 '17 at 09:07
  • Perfect answer! Worked like charm. – coderpc Oct 30 '17 at 04:28
127

Change this:

<li><a href="#one">One</a></li>

to this:

<li><a data-toggle="collapse" data-target=".navbar-collapse" href="#one">One</a></li>  

This simple change worked for me.

Ref: https://github.com/twbs/bootstrap/issues/9013

Joakim Johansson
  • 3,196
  • 1
  • 27
  • 43
FullStackDev
  • 1,569
  • 1
  • 10
  • 7
55

I had the same problem but caused by including twice bootstrap.js and jquery.js files.

On a single click the event was processed twice by both jquery instances. One closed and one opened the toggle.

raisercostin
  • 8,777
  • 5
  • 67
  • 76
  • As one who are modifying other's code, I was stuck in this issue for two days until I find this answer. Thanks! – JamesYTL Aug 11 '17 at 06:50
  • Thanks so much! Originally, I thought it was the ordering of the files (as some others had issue with), but then I thought it was a matter of wrongly labeled classes, but this was exactly my problem. – Dave Liu Jan 16 '18 at 23:24