How to close bootstrap dropdown menu on button click?
You can use the toggle
function on the .dropdown
that has the button inside:
$('#btnFilter').click(function() {
$(this).parents('.dropdown').find('button.dropdown-toggle').dropdown('toggle')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="dropdown">
<button class="btn dropdown-toggle col-md-3" type="button" data-toggle="dropdown">
Select Category
<span class="caret"></span>
</button>
<ul class="dropdown-menu col-md-7" onClick="event.stopPropagation();">
<div id="MaterialGridList" style="height:440px;"></div>
<button type="button" id="btnFilter" class="btn btn-sm btn-success">Filter</button>
</ul>
</div>
Just add the data-toggle="dropdown"
tag on the elements that you want to close the menu.
<button type="button" id="btnFilter" class="btn btn-sm btn-success" data-toggle="dropdown">Filter</button>