HTML Select Box or Drop down Box
Table of Content:
A drop - down select box allows users to select one item from a drop - down menu. Drop - down select boxes can take up far less space than a group of radio buttons.
Live Preview
The <select>
element is the containing element for a drop - down list box; it can take the attributes
shown in the table that follows:
Attribute | Purpose |
name | The name for the control. |
size | Can be used to present a scrolling list box, as you will see shortly. Its value would be the number of rows in the list that should be visible at the same time. |
multiple | Allows a user to select multiple items from the menu. If the attribute is not present, the user may select only one item. In earlier versions of HTML, this attribute did not have a value. However, to be valid XHTML it should be given the value of multiple (i.e., <select multiple= "multiple"> ). Note that the use of this attribute will change the presentation of the select box, as you will see in the section Selecting Multiple Options with the multiple Attribute later in this chapter. |
Another Example
Live Preview
Creating Scrolling Select Boxes
As I mentioned earlier, it ’ s possible to create scrolling menus where users can see a few of the options in
a select box at a time. In order to do this, you just add the size attribute to the <select>
element. The
value of the size attribute is the number of options you want to be visible at any one time.
Live Preview
Selecting Multiple Options with the multiple Attribute
The multiple attribute allows users to select more than one item from a select box. The value of the multiple attribute should be the word multiple in order for it to be valid XHTML (although earlier versions of HTML allowed this attribute to appear without a value). When you use this attribute it is always a good idea to tell people how to select multiple items: by holding down the control key and clicking on the items they want to select.
Live Preview
Grouping Options with the <optgroup>
Element
If you have a very long list of items in a select box, you can group them together using the < optgroup > element, which acts just like a container element for all the elements you want within a group.
Live Preview
An alternative option for grouping elements is to add an < option > element that carries the disabled attribute, which you will learn about shortly