Created
December 20, 2018 14:57
-
-
Save marcelo2605/f98de329d636a4099c9231e289ddc74b to your computer and use it in GitHub Desktop.
Open and close multiple dropdowns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.dropdown-menu { | |
display: none; | |
} | |
.dropdown.active .dropdown-menu { | |
display: block; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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