Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close bootstrap dropdown menu on button click?

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?

like image 327
Chris Avatar asked Oct 01 '16 22:10

Chris


People also ask

How do I stop dropdown menu to close menu items on clicking outside?

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.

How do I open Bootstrap dropdown menu on click rather than hover?

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.

How do I toggle a dropdown in Bootstrap?

Via data attributesAdd data-toggle="dropdown" to a link or button to toggle a dropdown.

How do I remove a dropdown from Bootstrap?

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.


2 Answers

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>
like image 65
Dekel Avatar answered Sep 30 '22 20:09

Dekel


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>
like image 42
Justine Laserna Avatar answered Sep 30 '22 21:09

Justine Laserna