How can I limit the number of shown options in an HTML <select>
drop down?
For example:
<select> <option value="1">1</option> <option value="2">2</option> ... <option value="20">20</option> </select>
How can I get the browser to show only the first five options and scroll down for the rest?
The hidden attribute hides the <option> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <option> element is not visible, but it maintains its position on the page.
We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element.
The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option.
You can use inline styles to add custome styling to <option> tags. For eg : <option style="font-weight:bold;color:#09C;">Option 1</option> This will apply the styles to this particular <option> element only. You can also use <option value="" disabled> <br> </option> to add a line-break between the options.
You can try this
<select name="select1" onmousedown="if(this.options.length>8){this.size=8;}" onchange='this.size=0;' onblur="this.size=0;"> <option value="1">This is select number 1</option> <option value="2">This is select number 2</option> <option value="3">This is select number 3</option> <option value="4">This is select number 4</option> <option value="5">This is select number 5</option> <option value="6">This is select number 6</option> <option value="7">This is select number 7</option> <option value="8">This is select number 8</option> <option value="9">This is select number 9</option> <option value="10">This is select number 10</option> <option value="11">This is select number 11</option> <option value="12">This is select number 12</option> </select>
It worked for me
You can use the size
attribute to make the <select>
appear as a box instead of a dropdown. The number you use in the size
attribute defines how many options are visible in the box without scrolling.
<select size="5"> <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> <option>12</option> </select>
You can’t apply this to a <select>
and have it still appear as a drop-down list though. The browser/operating system will decide how many options should be displayed for drop-down lists, unless you use HTML, CSS and JavaScript to create a fake dropdown list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With