I have a jquery function that swaps a bootsrap dropdown to a dropup, if the distance from the element to the bottom of the window is smaller than the height of the dropdown menu. It works fine, until the DOM gets updated and something gets added to the bottom of the window (let's say a new section or some menu gets expanded). The new distance from the element doesn't get accounted. I tried delegation with the .on method to check if the DOM gets updated but that did not fix the issue.
$('.dropdown').on('click', function(){
var elemDistanceToBottom = $(window).height() - $(this).offset().top;
if($('.dropdown-menu').height()>elemDistanceToBottom ){
$(this).closest(".dropdown-fix").addClass("dropup");
}
});