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.

Chapter 10 ✦ Select, Option, and Optgroup Objects (Chapter 26)<br />

Example<br />

I present Listing 26-9 in the hope that Microsoft and Netscape will eventually<br />

eradicate the bugs that afflict their current implementations of the label property.<br />

When the feature works as intended, Listing 26-9 demonstrates how a script can<br />

alter the text of option group labels. This page is an enhanced version of the background<br />

color setters used in other examples of this chapter. Be aware that IE5/Mac<br />

does not alter the last OPTGROUP element’s label, and NN6 achieves only a partial<br />

change to the text displayed in the SELECT element.<br />

Listing 26-9: Modifying OPTGROUP Element Labels<br />

<br />

<br />

Color Changer 3<br />

<br />

var regularLabels = [“Reds”,”Greens”,”Blues”]<br />

var naturalLabels = [“Apples”,”Leaves”,”Sea”]<br />

function setRegularLabels(list) {<br />

var optGrps = list.getElementsByTagName(“OPTGROUP”)<br />

for (var i = 0; i < optGrps.length; i++) {<br />

optGrps[i].label = regularLabels[i]<br />

}<br />

}<br />

function setNaturalLabels(list) {<br />

var optGrps = list.getElementsByTagName(“OPTGROUP”)<br />

for (var i = 0; i < optGrps.length; i++) {<br />

optGrps[i].label = naturalLabels[i]<br />

}<br />

}<br />

function seeColor(list) {<br />

var newColor = (list.options[list.selectedIndex].value)<br />

if (newColor) {<br />

document.bgColor = newColor<br />

}<br />

}<br />

<br />

<br />

<br />

<br />

Choose a background color:<br />

<br />

<br />

Light Red<br />

Medium Red<br />

Bright Red<br />

Dark Red<br />

<br />

<br />

Light Green<br />

Continued<br />

379<br />

OPTGROUP.label

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

Saved successfully!

Ooh no, something went wrong!