spacer

Dropdown Menus

 
spacer
You can prompt users for input by offering them a list of items displayed in a dropdown menu. The user clicks on the down arrow to open a list that 'drops down' and then clicks on a menu item in the list to select it. The items in the menu list are, by default, configured to be mutually exclusive of one another (like radio buttons) but can also be set to support multiple selections (like checkboxes). Dropdown menus offer the distinct advantage of being great space savers (they also look cool).

Dropdown menus are created by using the select element which acts as a container for a group of menu items, each of which is created by using the option element. Both the select element and the option element have start and end tags, e.g. <select>...</select> and <option>...</option>.


A SIMPLE DROPDOWN MENU

Here's a dropdown menu prompting the user to specify how they like their coffee:

I like my coffee:

SOURCE CODE

I like my coffee:<br><br>

<select name="coffee">

<option value="black">Black</option>

<option value="cream" selected>With cream</option>

<option value="sugar">With sugar</option>

<option value="regular">With cream & sugar</option>

<option value="crisp">Crisp (har har...)</option>

</select>




HOW DROPDOWN MENUS WORK

A group of menu items is created —each using the option element— and then placed within a containing select element. The start <select> tag contains the control name for the menu item group. In the above example, the control name is "coffee". Each menu item in the group has a different associated value specified by the value attribute. When a user selects a menu item, its value gets assigned as the current value of the menu item group's control name.



DEFAULT SELECTION

Web browsers will typically display the first menu item in the list as the default selection (the one that first appears in the menu window in its closed state). You can, however, specify which menu item this is by inserting the selected attribute. In the above example, the menu item "With cream" is set as the default selection.



MULTIPLE SELECTIONS

You can set the dropdown menu to permit the user to make multiple selections by inserting the multiple attribute in the <select> start tag. To see this in effect, please refer to the example below.



HOW MANY ROWS TO DISPLAY

The default closed state of a dropdown menu displays just a single line but you can set this to as many lines as you wish. This is accomplished by using the size="nn" attribute-value pair in the <select> start tag where nn equals the number of lines you wish to display. With the menu configured to multiple lines, it loses its 'dropdown' style and becomes a scrollable list of menu items. To see this in effect, please refer to the example below.



MAKING MENU SUBSECTIONS

An extensive list of menu items may serve the user better if it was organized into subsections. Each subsection is then given a title that describes the group of menu items it contains. This is accomplished by placing all the menu items you wish to define as a subsection between a set of <optgroup>...</optgroup> tags. The label="section_name" attribute-value pair is inserted in the <optgroup> start tag where section_name equals the title of the subsection.To see this in effect, please refer to the example below.



AN ADVANCED DROPDOWN MENU

In the following example, the menu employs subsections, displays five rows at a time and permits the user to make multiple selections. The menu item "Cream" is selected by default. The user makes multiple selections by pressing Ctrl on his keyboard while clicking on each menu item. (Menu items are 'deselected' in the same manner).

I like my coffee with:

SOURCE CODE

<select name="coffee2" size="5" multiple>

<optgroup label="Dairy">
<option value="skim">Skim Milk</option>
<option value="milk2">Milk (2%)</option>
<option value="cream" selected>Cream</option>
<option value="whipped">Whipped cream</option>
</optgroup>

<optgroup label="Sweetener">
<option value="sugar">Sugar</option>
<option value="honey">Honey</option>
</optgroup>

<optgroup label="Snack">
<option value="donut">A donut</option>
<option value="danish">A danish</option>
<option value="muffin">A muffin</option>
</optgroup>

</select>




Alright then, let's move on to creating the push buttons that will activate your web form...