HTML Select tag show vertical scroll with 10 option

RohitK picture RohitK · Jun 9, 2014 · Viewed 25.5k times · Source

I want to make a select box like this

enter image description here

with 10 select option, when I try to add more than 15 option it show vertical scroll bar, but not show when it have 10 option.

is there any way to achieve this.

Answer

Jai picture Jai · Jun 9, 2014

apply a size="10" to your select.

something like:

<select size="10">
   // all your options
</select>

You have to put some style to the label like border, background-image etc.

Something like this to be done:

<label id='choose' for='options'>Select options</label>
<br clear='all' />
<select id='options' size="10" style='display:none;'>
    // all the options
</select>

then use this jQuery code:

$('#choose').click(function(){
    $(this).siblings('select').toggle();
});

Demo @ Fiddle


Try with this:

$('#choose').click(function (e) {
    e.stopPropagation();
    $(this).siblings('select').css('width', $(this).outerWidth(true)).toggle();
});

$('#options').change(function (e) {
    e.stopPropagation();
    var val = this.value || 'Select options';
    $(this).siblings('#choose').text(val);
    $(this).hide();
});

As you commented:

when size is greater then 1 of select tag, its not showing blue background in hover, when i add background through css option:hover, its working in FF, but not in chrome and safari.

so you can mockup with this:

$('#options').find('option').on({
    mouseover: function () {
        $('.hover').removeClass('hover');
        $(this).addClass('hover');
    },
    mouseleave: function () {
        $(this).removeClass('hover');
    }
});

Demo with hover class.