I was wondering if any of you has what I am asking for ready, to save me from the trouble. What I am looking for is for a dropdown menu to have the dropup class automatically ad
I found the answer from Zoltán Tamási to be a good starting point but not enough when the dropdown should appear within a div with a vertical scrollbar. I needed the height of that div (the scrollHeight variable in the code below) to figure out if the dropdown will take to much space at the bottom.
jquery(document).on("show.bs.dropdown", ".dropdown", function () {
var $ul = jquery(this).children(".dropdown-menu");
var menuHeight = $ul.height()
var buttonHeight = jquery(this).children(".dropdown-toggle").height();
var scrollHeight = $ul.parent().offsetParent().height();
var menuAnchorTop = $ul.parent().position().top + buttonHeight;
// how much space would be left at the top
var spaceUp = (menuAnchorTop - buttonHeight - menuHeight);
// how much space would be left at the bottom
var spaceDown = scrollHeight - (menuAnchorTop + menuHeight);
// Switch to dropup if more space there
if (spaceDown < 0 && (spaceUp >= 0 || spaceUp > spaceDown))
jquery(this).addClass("dropup");
}).on("hidden.bs.dropdown", ".dropdown", function() {
// always reset after close
jquery(this).removeClass("dropup");
});