I have a bootstrap dropdown menu. I want it to be kept open when I click inside it, but closed when clicking the dropdown toggle button or outside of the menu.
It seems like the dropdown does not close when I type something in the input
but it closes when I click anywhere else inside the dropdown.
Q: How can I avoid this by using jQuery?
Below is my HTML:
<div id="navbar">
<nav class="navbar navbar-default navbar-static-top" role="navigation">
<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">MENU</a>
<div class="dropdown-menu">
<p>HELLO</p>
<input>
</div>
</li>
</ul>
</div>
</nav>
</div>
You can use the following JavaScript to manually open and close the dropdown menu:
$(function() {
$('.dropdown-toggle').on('click', function(event) {
$('.dropdown-menu').slideToggle();
event.stopPropagation();
});
$('.dropdown-menu').on('click', function(event) {
event.stopPropagation();
});
$(window).on('click', function() {
$('.dropdown-menu').slideUp();
});
});
$(function() {
$('.dropdown-toggle').on('click', function(event) {
$('.dropdown-menu').slideToggle();
event.stopPropagation();
});
$('.dropdown-menu').on('click', function(event) {
event.stopPropagation();
});
$(window).on('click', function() {
$('.dropdown-menu').slideUp();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div id="navbar">
<nav class="navbar navbar-default navbar-static-top" role="navigation">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">MENU</a>
<div class="dropdown-menu">
<p>Hello</p>
<input type="text">
</div>
</li>
</ul>
</nav>
</div>
From the question Avoid dropdown menu close on click inside, you can simply stop propagation from inside the dropdown like this:
$(document).on('click', '.allow-focus .dropdown-menu', function (e) {
e.stopPropagation();
});
$(document).on('click', '.allow-focus .dropdown-menu', function (e) {
e.stopPropagation();
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<div class="dropdown allow-focus">
<button id="dropdownLabel" type="button" class="btn btn-default"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown trigger
<span class="caret"></span>
</button>
<div class="dropdown-menu panel" aria-labelledby="dropdownLabel">
<div class="col-xs-12">
What's Your Name?
<input type="text" placeholder="Name" class="form-control" />
</div>
</div>
</div>
NOTE: To solve the opposite problem (of keeping open until clicked), you can refer to:
Keep Bootstrap Dropdown Open When Clicked Off
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With