15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Select, Option,<br />

and Optgroup<br />

Objects<br />

(Chapter 26)<br />

The SELECT element is the best space-saving device in the<br />

HTML form repertoire. Whether you choose the pop-up<br />

menu or scrolling list display style, your page can provide visitors<br />

with a visually compact list of literally hundreds of items<br />

from which to choose. From a scripter’s point of view, however,<br />

it is a complex item to manage, especially in older<br />

browsers.<br />

In truth, the SELECT element is an outer wrapper for the<br />

OPTION element items nested within. Each OPTION element<br />

contains the text that the user sees in the list, as well as a hidden<br />

value that may be more meaningful to a server database<br />

or client script. The difficulty with browsers prior to IE4 and<br />

NN6 is that reading the hidden value of the currently chosen<br />

item in the list requires an extensive reference to not only the<br />

SELECT element, but to the item in the array of OPTION element<br />

objects. To reach that specific item, the script uses a reference<br />

to the SELECT object’s selectedIndex property as<br />

the options array index. Newer browsers simplify the matter<br />

by providing a single value property for the SELECT object<br />

that returns the value of the currently selected item (or of the<br />

first item when multiple choices are allowed).<br />

Many browser versions provide script facilities for modifying<br />

the content of a SELECT list. But the effect is not perfect in<br />

browsers that don’t also reflow the page to reflect the potentially<br />

resized width of the list.<br />

A user interface debate rages about whether a SELECT list,<br />

whose purpose is obviously intended to direct site navigation,<br />

should navigate immediately upon making a choice or if the<br />

user should also click on an explicit “Go” button next to the<br />

list. The former is faster for the impatient visitor, but the latter<br />

doesn’t shoot off to an undesired page when the user<br />

makes a wrong selection. Good luck with that decision.<br />

10<br />

C H A P T E R<br />

✦ ✦ ✦ ✦<br />

In This Chapter<br />

Triggering action<br />

based on a user’s<br />

selection in a pop-up<br />

or select list<br />

Reading hidden and<br />

visible values of<br />

OPTION element<br />

items<br />

Scripting SELECT<br />

objects that allow<br />

multiple selections<br />

✦ ✦ ✦ ✦

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!