i have been working on a dropdown replacement jquery plugin to combat this problem. As of this post, it is almost indistinguishable from a native dropdown in terms of look and functionality.
(update:)
the jquery plugin page seems to no longer work. I will probably not put my plugin on their new site when they get it working, so feel free to use the programmingdrunk.com link for demo/download
Actually you kind of can! Don't hassle with javascript... I was just stuck on the same thing for a website I'm making and if you increase the 'font-size' attribute in CSS for the tag then it automatically increases the height as well. Petty but it's something that bothers me a lot ha ha
There are three problems with this solution. 1) There is a quick flash of all the elements shown during the first mouse click. 2) The position is set to 'absolute' 3) Even if there are less than 'n' items the dropdown box will still be for the size of 'n' items.
NO. It's not possible to change height of a select dropdown because that property is browser specific.
However if you want that functionality, then there are many options. You can use bootstrap dropdown-menu and define it's max-height property. Something like this.
$('.dropdown-menu').on( 'click', 'a', function() {
var text = $(this).html();
var htmlText = text + ' <span class="caret"></span>';
$(this).closest('.dropdown').find('.dropdown-toggle').html(htmlText);
});
My approach is different: I calculate and set the size of the select to match the available space.
I start with a smaller select than the available space
I determine the actual height of each option by increasing the size of the select and seeing how its offsetHeight changes; this value is browser dependent
I calculate how many more options can fit
I increase the size of the select to a value that will make it fit
// Make the select fit in the div`
// The looks of a select are set by the browser, not by the client
// Therefore, they change from browser to browser, and even with various revs of a given browser
// This cannot be done at init, because the height of each option changes later
// Instead, it must be done later, after the select has rendered
if (!seriesSelect.fitted) { // If we haven't done it yet
seriesSelect.fitted = true; // Flag it, so we don't do it again
// Determine the actual height of each option in the select
var maxHeight = seriesSelect.parentElement.parentElement.offsetHeight;
var startHeight = seriesListBox.offsetHeight;
seriesSelect.size = seriesSelect.size +1;
var endHeight = seriesListBox.offsetHeight;
var lineHeight = endHeight - startHeight;
// Calculate how many more options can fit
var extraHeight = maxHeight - endHeight;
var extraItems = Math.floor(extraHeight / lineHeight);
// Set the number of options to the max that will fit
seriesSelect.size = seriesSelect.size + extraItems;
}