Skip to content

Instantly share code, notes, and snippets.

@strika
Created January 24, 2017 10:53
Show Gist options
  • Save strika/47544bf530b316f01159590c5ebc3d7e to your computer and use it in GitHub Desktop.
Save strika/47544bf530b316f01159590c5ebc3d7e to your computer and use it in GitHub Desktop.
A utility for keeping a Bootstrap drop down menu open after a link is clicked.
// A utility for keeping a Bootstrap drop down menu open after a link is
// clicked
//
// Usage:
//
// <div class="dropdown">
// <a href="" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
// Dropdown trigger <span class="caret"></span>
// </a>
//
// <ul class="dropdown-menu" aria-labelledby="dLabel">
// <li><a href="">Edit</a></li>
// <li><a href="" keep-menu-open="true">Delete</a></li>
// </ul>
// </div>
$(".dropdown .dropdown-menu a").on("click", function(e) {
var keepMenuOpen = $(this).data("keep-menu-open"),
$dropdown = $(this).parents(".dropdown");
$dropdown.data("keep-menu-open", keepMenuOpen);
});
$(".dropdown").on("hide.bs.dropdown", function(e) {
var keepMenuOpen = $(this).data("keep-menu-open");
$(this).removeData("keep-menu-open");
return keepMenuOpen !== true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment