How to close an open collapsed navbar when clicking outside of the navbar element in Bootstrap 3?


How can I close an open collapsed navbar on clicking outside of the navbar element? Currently, the only way to open or close it is by clicking on the navbar-toggle button.

See here for an example and code:

So far, I have tried the following which doesn’t seem to work:

jQuery(document).click(function() {


jQuery('.navbar').click(function(event) {


Have a look that:

$(document).ready(function () {
    $(document).click(function (event) {
        var clickover = $(;
        var _opened = $(".navbar-collapse").hasClass("navbar-collapse in");
        if (_opened === true && !clickover.hasClass("navbar-toggle")) {

Your fiddle works with that:

Its a modified version of this answer, which lacks the animation and is also a tiny bit more complicated.

I know, invoking the click() isn’t very elegant, but collapse('hide') did not work for me either, and i think the animation is a bit nicer than adding and removing the classes hardly.

Answered By – nozzleman

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.