31

my selectbox is showing scroll when number of options are more than 20 i just want to show scrol in when options are more than 10

 <select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
  </select>
Atif Azad
  • 697
  • 2
  • 9
  • 21

9 Answers9

40

Try adding this to the <select> element:

onfocus='this.size=10;' 
onblur='this.size=1;' 
onchange='this.size=1; this.blur();

like:

   <select onfocus='this.size=10;' onblur='this.size=1;' 
        onchange='this.size=1; this.blur();'>

SAMPLE DEMO

John
  • 1
  • 13
  • 98
  • 177
reuelab
  • 1,976
  • 1
  • 19
  • 28
  • 1
    @VinceCgto your way shows all the 10 options all at once.. the OP wants to show only one then show 10 on focus. – reuelab May 08 '14 at 06:51
  • if i am select same value again..on change event not work so select size attribute no re sized. how to re size =1 when select a same value?. – Palaniraja May 31 '18 at 09:21
  • This works really well. Had to add `style='height:auto'` to `select` to make the select box expand for some cases. – Soujirou May 31 '19 at 00:25
22

Seriously guys, there is a size attribute for the select element:

<select size="10">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
</select>

FIDDLE: http://jsfiddle.net/UR46N/

John
  • 1
  • 13
  • 98
  • 177
Vince C
  • 868
  • 6
  • 16
  • 1
    This is how I interpreted it (and still do after re-reading your question). Oh well, glad you solved your problem. – Vince C May 08 '14 at 07:02
1

In Case of setting size (height/width) and fitting the selection list in the pop up then try the following in VFPage- Salesforce:

<apex:selectList onclick="this.size=5;" required="true" id="form-element-05" value="{!scheduleBatchWrapper.hour}" size="1" styleClass="slds-select selectObj  slds-m-left_medium slds-m-top_medium schTime" style="max-width:50%; margin-bottom:5% margin-top:5%,max-height:20%" >
                                                                            <apex:selectOptions value="{!scheduleBatchWrapper.hoursList}"></apex:selectOptions>
                                                                        </apex:selectList>

// here onclick="this.size=5;" will set the size to 5 when you click on the selectList. //it works fine for Chrome,Firefox,Edge.

Thanks

1

As much as I see, none of the former answers explain the Options box. As much as I know, we don't have accessibility to manipulate options in a select box. We just can change the colors and background color at the same time, add a new font and that's all we could do. If we add padding or margin to the Options, the drop-down list will be still the same as its initial condition.

The only way to do so is to design a select-box on your own( by using radio buttons, prevent their default style, and add a background color to it. )

0

check this .i have implemented scroll to your select box. http://plnkr.co/edit/XiKZ4X2DGYTJDjJX2xvw?p=preview

Pranav
  • 666
  • 3
  • 7
-1
<select>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
</select>

option {
    padding: 5px 0;
}
Senthil Kumar
  • 145
  • 1
  • 4
-1

I am using Bootstrap and this answer helped me.

.dropdown-menu {
    max-height: 280px;
    overflow-y: auto;
}
Anupam
  • 14,950
  • 19
  • 67
  • 94
  • Thanks for the downvote. Would really appreciate any comment with a downvote so I could realise what was wrong – Anupam Nov 05 '21 at 09:16
-2

Why dont you add CSS style to specify the width and height of the select. Check the article--> http://cssdeck.com/labs/styling-select-box-with-css3

Aradhna
  • 973
  • 10
  • 20
-2

use below css

select{ padding:5px;}
Amit
  • 1,841
  • 1
  • 19
  • 36