home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeHTML & XHTML: The Definitive GuideSearch this book

9.8. Multiple Choice Elements

Checkboxes and radio buttons give you powerful means for creating multiple-choice questions and answers, but they can lead to long forms that are tedious to write and put a fair amount of clutter onscreen. The <select> tag gives you two compact alternatives: pull-down menus and scrolling lists.

9.8.1. The <select> Tag

By placing a list of <option>-tagged items inside the <select> tag of a form, you magically create a pull-down menu of choices.

<select>

Function:

Create single- and multiple-choice menus

Attributes:

CLASS

ONKEYPRESS

DIR

ONKEYUP

DISABLED

ONMOUSEDOWN

ID

ONMOUSEMOVE

LANG

ONMOUSEOUT

MULTIPLE

ONMOUSEOVER

NAME

ONMOUSEUP

ONBLUR

SIZE

ONCHANGE

STYLE

ONCLICK

TABINDEX

ONDBLCLICK

TITLE

ONFOCUS

ONKEYDOWN

End tag:

</select>; never omitted

Contains:

select_content

Used in:

form_content

As with other form tags, the name attribute is required and used by the browser when submitting the <select> choices to the server. Unlike radio buttons, no item is preselected, so if none is selected, no values are sent to the server when the form is submitted. Otherwise, the browser submits the selected item or collects multiple selections, separated with commas, into a single parameter list and includes the name attribute when submitting <select> form data to the server.

9.8.2. The <option> Tag

Use the <option> tag to define each item within a <select> form control.

<option>

Function:

Define available options within a <select> menu

Attributes:

CLASS

ONMOUSEDOWN

DIR

ONMOUSEMOVE

DISABLED

ONMOUSEOUT

ID

ONMOUSEOVER

LABEL

ONMOUSEUP

LANG

SELECTED

ONCLICK

STYLE

ONDBLCLICK

TITLE

ONKEYDOWN

VALUE

ONKEYPRESS

ONKEYUP

End tag:

</option>; usually omitted in HTML

Contains:

plain_text

Used in:

select_content

The browser displays the <option> tag's contents as an element within the <select> tag's menu or scrolling list, so the content must be plain text only, without any other sort of markup.

9.8.3. The <optgroup> Tag

Menus of choices in forms can be quite large, making them difficult to display and use. In these cases, it is helpful to group related choices, which can then be presented as a set of nested, cascading menus to the user. New in HTML 4.0, the <optgroup> tag brings this capability to HTML and XHTML forms, albeit limited.

Use the <optgroup> tag only within a <select> tag, and it may only contain <option> tags. The browser creates submenus for each <optgroup> tag within the main <select> menu. For example, with HTML you might use <optgroup> to present a form menu of states organized by region:

<select name=state>
   <optgroup label=Northeast>
      <option>Maine
      <option>New Hampshire
      ...
   </optgroup>
   <optgroup label=South>
      <option>Georgia
      <option>Florida
      ...
   </optgroup>
   ...
</select>

Since no browser yet fully supports the <optgroup> tag (the popular browsers simply display them as a scrolling menu), we can't show you what this menu might look like. However, it will probably look and feel much like the familiar pull-down menus that are a common feature of most graphical user interfaces. When selected with the mouse or keyboard, the optgroup opens into one or more menus. For instance, our "state" example probably will have submenus labeled "Northeast," "South," and so on, each of which can be pulled open to reveal a list of included states.

Regrettably, the biggest drawback to the <optgroup> tag is that it cannot be nested, limiting you to one level of submenus. Presumably this restriction will be lifted in a future version of XHTML.

<optgroup>

Function:

Group related <option> elements within a <select> menu

Attributes:

CLASS

ONKEYUP

DIR

ONMOUSEDOWN

DISABLED

ONMOUSEMOVE

ID

ONMOUSEOUT

LABEL

ONMOUSEOVER

LANG

ONMOUSEUP

ONCLICK

STYLE

ONDBLCLICK

TITLE

ONKEYDOWN

ONKEYPRESS

End tag:

</optgroup>; may be omitted in HTML

Contains:

optgroup_contents

Used in:

select_content



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.