134

This is not a submenu dropdown, the category is class li as in the picture:

enter image description here

By selecting a category from the responsive menu (the template is just one page), I want to hide nav collapse automatically when clicking. Also stroll to use as navigation, since the template has only one page. I seek a solution that does not affect it, here is the HTML code of menu:

    <!-- NAVBAR
  ================================================== -->
<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
      <a class="btn btn-navbar" 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="brand" href="#">Carousel Demo</a>
      <div class="nav-collapse">
        <ul class="nav" >
          <li class="active"><a href="#home">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#portfolio">Portfolio</a></li>
          <li><a href="#services">Services</a></li>
          <li><a href="#contact">Contact</a></li>
          <!-- dropdown -->
        </ul>
        <!-- /.nav -->
      </div>
      <!--/.nav-collapse -->
    </div>
    <!-- /.container -->
  </div>
  <!-- /.navbar-inner -->
</div>
<!-- /.navbar -->
Community
  • 1
  • 1
Tim Vitor
  • 1,473
  • 3
  • 11
  • 7
  • and my solution working or not? – WooCaSh May 21 '13 at 23:51
  • @WooCaSh Worked rather the order of their solution custom.js was wrong, I noticed $ ('nav a'). On ('click', function () {and in my case it is a class, then $ ('. nav a '). on (' click ', function () {. thank you, you helped me a lot! – Tim Vitor May 22 '13 at 01:18
  • possible duplicate of [Close responsive navbar automatically](http://stackoverflow.com/questions/14248194/close-responsive-navbar-automatically) – chrylis -cautiouslyoptimistic- Dec 16 '13 at 04:59
  • If you don't want to try and compare all these solutions here yourself, [edit #1 from this answer](http://stackoverflow.com/a/23171593/89818) is what you should do. – caw Sep 30 '14 at 13:14
  • You could see my answer here http://stackoverflow.com/questions/14248194/close-responsive-navbar-automatically#answer-26191027 – Camel Oct 04 '14 at 08:44
  • **Bootstrap 4** method: https://stackoverflow.com/questions/42401606/how-to-hide-collapsible-bootstrap-4-navbar-on-click/42401686 – Carol Skelly Mar 06 '18 at 17:09
  • @TimVitor I have added an updated answer, see: https://stackoverflow.com/a/75808109/10686802 – Remzi Cavdar Mar 22 '23 at 03:08

31 Answers31

166

try this:

$('.nav a').on('click', function(){
    $('.btn-navbar').click(); //bootstrap 2.x
    $('.navbar-toggle').click(); //bootstrap 3.x by Richard
    $('.navbar-toggler').click(); //bootstrap 4.x
});
Community
  • 1
  • 1
WooCaSh
  • 5,180
  • 5
  • 36
  • 54
  • 1
    This worked great. Thanks WooCaSh. Also just suggested adding a "." to the code before $('nav a') => $('.nav a'). [Class selector was missing a dot "."] – Clain Dsilva Aug 05 '13 at 06:44
  • 7
    FYI, this doesn't work in all cases. If the window is re sized and it brings up the mobile menu it will be open by default. – Andrew Boes Sep 13 '13 at 19:59
  • 8
    UPDATE: Usual selector for bootstrap 3 is .navbar-toggle, so $(".navbar-toggle").click(); – Richard Oct 10 '13 at 19:43
  • 1
    If you have dropdowns in your navbar, you don't want to collapse when clicking on the dropdown. This worked for me: $('.nav :not(.dropdown) a').on(... – Dr. Mike Hopper Aug 11 '14 at 00:29
  • 41
    This is causing issues in the navbar. Better to use $('.nav-collapse').collapse('hide'); on click event rather than making the toggle button click. – Rohan Oct 22 '14 at 23:02
  • Thanks @Ronan, after trying all the solutions, yours is the best one on mobile and desktop. You saved my day and should get the "correct answer". – xBill Oct 31 '14 at 19:34
  • 7
    When the navbar is open in "desktop" mode it will flicker or close then reopen when using this. – mlapaglia Feb 16 '15 at 19:41
  • jQuery('.navbar-collapse').addClass('collapse'); was the most effective method in Bootstrap 3. – Michael May 11 '16 at 21:28
  • when menu is open you can see there is "in" class active ` – Braham Dev Yadav May 01 '17 at 19:03
  • The collapsing of the navigation should be done with bootstrap. You've build it with bootstrap, fix it with bootstrap if possible: look at Zu1779's answer and the comments. Way cleaner and makes a way more sense. The way to handle it is available in bootstrap itself, why ignore that? – Nrzonline Sep 12 '17 at 09:24
135

I just replicate the 2 attributes of the btn-navbar (data-toggle="collapse" data-target=".nav-collapse.in") on each link like this:

<div class="nav-collapse">
  <ul class="nav" >
    <li class="active"><a href="#home" data-toggle="collapse" data-target=".nav-collapse.in">Home</a></li>
    <li><a href="#about" data-toggle="collapse" data-target=".nav-collapse.in">About</a></li>
    <li><a href="#portfolio" data-toggle="collapse" data-target=".nav-collapse.in">Portfolio</a></li>
    <li><a href="#services" data-toggle="collapse" data-target=".nav-collapse.in">Services</a></li>
    <li><a href="#contact" data-toggle="collapse" data-target=".nav-collapse.in">Contact</a></li>
  </ul>
</div>

In the Bootstrap 4 Navbar, in has changed to show so the syntax would be:

data-toggle="collapse" data-target=".navbar-collapse.show"

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
Zu1779
  • 2,178
  • 1
  • 18
  • 17
  • 9
    Works well on mobile, but causes the navbar collapse animation on desktop too, which is undesirable. – James Brewer Feb 18 '15 at 02:10
  • Gives a navbar animation (quick expand/collapse) on navbar item clicks. This happens even when the collapse button is not shown. Not ideal. – lostintranslation Mar 20 '15 at 16:05
  • 35
    @JamesBrewer To avoid the collapse animation on the desktop version add `.in` to the data-target: `data-toggle="collapse" data-target=".nav-collapse.in"` – Daghall Mar 21 '15 at 22:16
  • I like this no-manual-script version. Better with other frameworks – Stalin Gino Dec 08 '15 at 08:41
  • 23
    Don't forget to replace .nav-collapse with .navbar-collapse for bootstrap 3 – kuceram Mar 29 '16 at 14:37
  • 3
    This should be the accepted answer! Thank you for posting it! – trixtur Jul 18 '16 at 04:16
  • Would adding this to the body element be a bad idea if I wanted to collapse the menu if someone clicks anywhere on the page? – scorgn Aug 01 '17 at 22:08
  • 2
    Putting this on
      works for all the
    • under it, you can avoid repeating this line.
    – Zeus Nov 14 '18 at 06:36
  • Awesome! I was adding Jquery and had issues with animation and you saved the day! Cheers – SKG Mar 12 '19 at 16:30
  • 1
    @Zeus approach was the only one that worked 100% on both! Thanks! – dNurb Apr 25 '19 at 18:05