15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

370<br />

SELECT.multiple<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

<strong>Examples</strong> Highlights<br />

✦ To harvest the values of all selected items in a multiple list, your script needs<br />

to cycle through the SELECT element’s options array and inspect the<br />

selected property of each, as shown in Listing 26-4.<br />

✦ Scripts can also retrieve the text of the selected item, instead of the hidden<br />

value. Compare two similar applications that work with the text (Listing 26-5)<br />

and value (Listing 26-6) properties.<br />

✦ Listings 26-5 and 26-6 show the backward-compatible, long reference to<br />

retrieve a chosen option’s details. The modern alternative accompanies the<br />

example for the SELECT.value property.<br />

✦ See Listing 26-8 for another example of triggering a script via the onChange<br />

event handler of a SELECT object.<br />

✦ Implementations of the OPTGROUP element object may need improvement<br />

before Listing 26-9 behaves as it should to modify hierarchical labels within a<br />

SELECT list.<br />

SELECT Element Object<br />

Properties<br />

length<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓<br />

Example<br />

See Listing 26-1 in Chapter 26 of the <strong>JavaScript</strong> <strong>Bible</strong> for an illustration of the way<br />

you use the length property to help determine how often to cycle through the<br />

repeat loop in search of selected items. Because the loop counter, i, must start at 0,<br />

the counting continues until the loop counter is one less than the actual length<br />

value (which starts its count with 1).<br />

multiple<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓<br />

Example<br />

The following statement toggles between single and multiple selections on a<br />

SELECT element object whose SIZE attribute is set to a value greater than 1:

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

Saved successfully!

Ooh no, something went wrong!