How to close an open collapsed navbar when clicking outside of the navbar element or clicking on a link in Bootstrap 4?
I think mostly what you need to do it change the class names, for instance in the code i can't see a class named "navbar-collapse":
$(document).on('click',function(e) {
if( $(e.target).attr('class') != 'navbar' ) {
$('#navbarResponsive').removeClass('in');
}
});
Bootstrap 3.3.7 How to close the navigation bar by clicking outside?
You should check where did the click happen. If the click is outside the navbar then toggle it, otherwise don't do it.
Your javascript code would become:
(function ($) {
'use strict';
$(document).click(function (event) {
if (!($(event.target).is('#navbar-collapse-first *'))
$('.navbar-collapse-first').collapse('hide');
});
}(jQuery));
Closing an open collapsed navbar when clicking outside in Booststrap 4
Here is solution to your problem:
$(document).ready(function () {
$(document).click(function (event) {
var clickover = $(event.target);
var _opened = $(".collapse").hasClass("collapse in");
if (_opened === true && !clickover.hasClass("navbar-toggler")) {
$("button.navbar-toggler").click();
}
});
});
Codepen: http://codepen.io/anon/pen/xgbmqr
Its a modified version of this answer
Bootstrap: How to close an open collapsed navbar when clicking outside of the MENU?
You can use this to collapse if not clicking a link: fiddle
$(document).click(function(e) {
if (!$(e.target).is('a')) {
$('.collapse').collapse('hide');
}
});
How to hide collapsible Bootstrap navbar on click
Update 2021 - Bootstrap 5 (beta)
Use javascript to add a click event listener on the menu items to close the Collapse navbar..
const navLinks = document.querySelectorAll('.nav-item')
const menuToggle = document.getElementById('navbarSupportedContent')
const bsCollapse = new bootstrap.Collapse(menuToggle)
navLinks.forEach((l) => {
l.addEventListener('click', () => { bsCollapse.toggle() })
})
BS5 demo javascript method
Or, Use the data-bs-toggle
and data-bs-target
data attributes in the markup on each link to toggle the Collapse navbar...
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto">
<li class="nav-item active">
<a class="nav-link" href="#" data-bs-toggle="collapse" data-bs-target=".navbar-collapse.show">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" data-bs-toggle="collapse" data-bs-target=".navbar-collapse.show">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" data-bs-toggle="collapse" data-bs-target=".navbar-collapse.show">Disabled</a>
</li>
</ul>
<form class="d-flex my-2 my-lg-0">
<input class="form-control me-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</div>
</nav>
BS5 demo data-attributes method
Update 2019 - Bootstrap 4
The navbar has changed, but the "close after click" method is still the same:
BS4 demo jQuery method
BS4 demo data-toggle
method
Bootstrap 3 (original answer)
You can add the collapse
component to the links like this..
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#home" data-toggle="collapse" data-target=".navbar-collapse.show">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#about-us" data-toggle="collapse" data-target=".navbar-collapse.show">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#pricing" data-toggle="collapse" data-target=".navbar-collapse.show">Pricing</a>
</li>
</ul>
BS3 demo using 'data-toggle' method
Or, (perhaps a better way) use jQuery like this..
$('.navbar-nav>li>a').on('click', function(){
$('.navbar-collapse').collapse('hide');
});
BS3 demo using jQuery method
How to close the menu when I click outside?
Keep it simple: if you want to close .navbar-collapse when you click outside .navbar-collapse just write that.
/// When you click everywhere in the document
$(document).click(function (event) {
/// If *navbar-collapse* is not among targets of event
if (!$(event.target).is('.navbar-collapse *')) {
/// Collapse every *navbar-collapse*
$('.navbar-collapse').collapse('hide');
}
});
You don't even need to use IDs
Related Topics
Change Specific Button Text on Click Inside Ngfor
React Native: Image Not Displaying With Dynamic/Local Uri
Vuejs Error, Invalid Prop: Type Check Failed for Prop. Expected Date, Got Number With Value
How to Attach Multiple Images in a Embed
How to Create a Remember Me Function in Login Without Using Form in JavaScript or Jquery
Javascript, Viewing [Object Htmlinputelement]
Ng2-Smart-Table Bind 'Add New' Button Event to an External Button
Formatting a Number to Have Commas At Every 1000 Factor
Onclick Event Not Working on Google Chrome
Add and Remove Values Inside Array in Change Event of Checkbox
Slick Carousel - Force Slides to Have the Same Height
Reactjs: Expected Onclick Listener to Be a Function, Instead Got Type String
Check If a String Is HTML or Not
Get Image from Json File Using JavaScript and Display in HTML Img Tag
How to Find Nearest Location Using Latitude and Longitude from a Json Data
How to Prevent iOS Keyboard from Pushing the View Off Screen With CSS or Js