Javascript Reference - HTML DOM Select size Property








The size property sets or gets the size attribute of a drop-down list.

The size attribute specifies the number of visible options in a drop-down list.

Browser Support

size Yes Yes Yes Yes Yes

Syntax

Return the size property.

var v = selectObject.size 

Set the size property.

selectObject.size=number




Property Values

Value Description
number Specifies the number of visible options in a drop-down list.

Return Value

A Number type representing the number of visible options in the drop-down list

Example

The following code shows how to get the number of visible options in a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--from  w ww .  ja v a  2 s .c om-->
<select id="mySelect" size="5">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("mySelect").size;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the number of visible options in a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--from w w w  . j  a v a2 s.c o  m-->
<select id="mySelect" size="1">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
  <option>E</option>
  <option>F</option>
  <option>G</option>
</select>
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
     document.getElementById("mySelect").size = "4";
}
</script>

</body>
</html>

The code above is rendered as follows: