Skip to content

Instantly share code, notes, and snippets.

@marcelo2605
Created December 20, 2018 14:57
Show Gist options
  • Save marcelo2605/f98de329d636a4099c9231e289ddc74b to your computer and use it in GitHub Desktop.
Save marcelo2605/f98de329d636a4099c9231e289ddc74b to your computer and use it in GitHub Desktop.
Open and close multiple dropdowns
<div class="dropdown">
<button class="dropdown-toggle" type="button">Dropdown</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Item 1</a>
<a class="dropdown-item" href="#">Item 2</a>
</div>
</div>
.dropdown-menu {
display: none;
}
.dropdown.active .dropdown-menu {
display: block;
}
// open/close dropdown menu
$('.dropdown-toggle').on('click', function(){
$(this).parent().toggleClass('active');
});
// close dropdown menu
var $menu = $('.dropdown');
$(document).mouseup(function(e){
if ((!$menu.is(e.target) && $menu.has(e.target).length === 0) || ($menu.not($(e.target).parent()).hasClass('active') && !$(e.target).hasClass('dropdown-item')) ) {
$menu.removeClass('active');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment