jim.shamlin.com

Select

Content


Basics

A select object is one of the few form elements that is not implemented by using the INPUT tag. The code for a typical select, in which items are contained in a drop-down menu, is:

<SELECT>
  <OPTION>option a</OPTION>
  <OPTION>option b</OPTION>
  <OPTION>option c</OPTION>
  <OPTION>option d</OPTION>
</SELECT>  

Another option, which is unusual, is an open list that enables the user to scroll to see multiple options at once. In some instances, a list is set up to enable the user to select multiple items by using command-click.

In either instance, there should be at least two options for the user to choose within the select object. Also, an option should not be used outside of the context of a select object, nor should any HTML code be used among or within the options.


Usage

A select object provides a drop-down menu or scrolling list of options for the user to choose from. It's also worth noting that the user can navigate the content of the list by using the keyboard: arrow keys to scroll through the list, or a keystroke to jump to the first option that begins with a given character.


Processing

A normal select object passes a name=value pair to the server in which the value represents the item that was selected at the instant the form was submitted.

If the user is able to select multiple items, it gets a bit weird. Most browsers will pass multiple name=value pairs, one for each item selected, like this"

select_name=value&select_name=value&select_name=value

This is a bit of a bother, as it requires you to go back to the input string and parse it again, to put the items into an array, thus:

@in = split(/[&;]/,$in);   # presuming $in has been loaded with the input string
foreach $i (0 .. $#in) {
   $in[$i] =~ s/\+/ /g;
   ($key, $val) = split(/=/,$in[$i],2);
   $key =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
   next unless ($key eq 'select_name');
   $val =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
   $select_name[@select_name] = $val;
   }

After running this, @select_name is an array of all the values selected in the select_name element.


HTML Attributes

The select object itself has the following attributes:

Each option has the following attributes:

An option group has the following attributes:


JavaScript

This covers the attributes of the select object and its options. I've not found a good resource for option groups, and haven't needed to goof with them as yet.

Properties

Methods

Event Handlers


Misc Notes

Whether to use a selector or a set of radio buttons will depend on the specific nature of the data you're looking to collect. The advantage to a selector is that it tends to be more compact, which is important if there are a lot of options to both design and accessibility.

A good practice is to provide options in alphabetical order to facilitate keyboard navigation - the user can click a key to jump to the first option beginning with that character, then again for the next. If there are few options, this may not be that valuable, and a better approach might be to place the most commonly-chosen items nearest the top to reduce the amount of tabbing or mouse-movement needed.

Note that the keyboard navigation is single-character. If the user types 'k' then 'e', they will jump to the first option starting with 'k' then jump to the first option starting with 'e', and not the first option starting with 'ke'. This is counter-intuitive, but it's a standard behavior.

The MULTIPLE attribute is not designed to take any argument. Some sources have suggested you can pass a number as an argument to indicate the maximum number of items a user can choose, but this is not standard and may not be widely supported. A JavaScript hack will be needed to restrict the number of items the user may select.

Also on MULTIPLE, if the number of options is small, it's generally better to use a series of checkboxes than a scrolling list, as the former is more intuitive.