I have a bootstrap dropdown menu that I open and got it staying open while I work with a grid that I placed inside of it, I can close the dropdown when I click outside of the dropdown area, but I want to close it down on a button click event..
Here is my html for the dropdown
<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>
as you can see on the onClick event of the dropdown menu, it keeps it open. So what I would like to do is close it on the btnFilter click event. Any idea's?
Answer: Use the jQuery on() method You can use the jQuery click() method in combination with the on() method to hide the dropdown menu when the user click outside of the trigger element.
Answer: Use the jQuery hover() method By default, to open or display the dropdown menu in Bootstrap you have to click on the trigger element. However, if you want to show the dropdown on mouseover instead of click you can do it with little customization using the CSS and jQuery.
Via data attributesAdd data-toggle="dropdown" to a link or button to toggle a dropdown.
Every Bootstrap dropdown button or link has an ::after selector in CSS. ::after selector is often used to insert some text after the content of the element. In this case, the content is a dropdown arrow. To remove it, just make the content go 'none'. One can use this feature to create navigation menus in top navbars.
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>
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